(Solved) Why is my Toast not showing Ex 6-5

Tom Farrell
None
EDIT; Nevermind! Typo on my part. I spelled onPostExecute incorrectly, so never actually did override the method. :)
Hi. Love the book, so far. I am having a problem with the code in example 6-5 with my Toast messages not showing up. I know that in my subclassed AsyncTask that the code in DoInBackground is executing, as my online statuses are updating, but it seems like PostExecute is not being called. Here's my code. If anyone can clear this up for me, that would be wonderful - Tom
Code:
public class StatusActivity extends Activity implements OnClickListener {
private static final String TAG = "StatusActivity";
private EditText editText;
private Button buttonUpdate;
private Twitter twitter;
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
editText = (EditText)findViewById(R.id.editText);
buttonUpdate = (Button)findViewById(R.id.buttonUpdate);
buttonUpdate.setOnClickListener(this);
twitter = new Twitter("u", "p");
twitter.setAPIRootUrl("http://yamba.marakana.com/api");
}
@Override
public void onClick(View v) {
new PostToTwitter().execute(editText.getText().toString());
Log.d(TAG, "onclick");
}
public class PostToTwitter extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... status) {
try {
Twitter.Status s = twitter.updateStatus(status[0]);
return s.toString();
}
catch (TwitterException e) {
Log.d(TAG, e.toString());
return "could not post!";
}
}
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate();
}
protected void onPostExceute(String result) {
Toast message = new Toast(getApplicationContext());
message.setText(result);
message.setDuration(Toast.LENGTH_LONG);
message.show();
}
}//end PostToTwitter
}//end StatusActivity
Edited one time. Last edit by Tom Farrell on May 26, 2011 at 5:09:32 PM (about 2 years ago).

Marko Gargenta
@MarkoGargenta
Marakana, Inc.
:) That's why @Override helps.

Juan Menendez
Home
I have the same problem but I see no typo in my code, in fact, it's been pasted directly from the book. I have added a log.i() message to see whether the method was visitedd or not, and it seems like the method is NOT executed... but why?
Code:
class PostToTwitter extends AsyncTask<String, Integer, String> {
// Called to initiate the background activity
@Override
protected String doInBackground(String... statuses) {
try {
Twitter.Status status = twitter.updateStatus(statuses[0]);
return status.text;
} catch (TwitterException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return "Failed to post";
}
}
// Called when there's a status to be updated
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Not used in this case
}
// Called once the background activity has completed
@Override
protected void onPostExecute(String result) {
Toast.makeText(StatusActivity2.this, result, Toast.LENGTH_LONG).show();
Log.i(TAG, "onPostExecute");
}

Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Does doInBackground() execute? If not, does it print error? If it doesn't do either, do you ever create and execute the PostToTwitter talk at all?

Juan Menendez
Home
It did post the twit into the timeline but the onPostExecute() method didn't execute. I must have missed something someplace. I rewrited everything from scratch and it runs well now. I wonder what was the mistake though, just in case something like that happens again. Maybe the mistake was in the doInBackground() method that prevented it from returning properly...who knows :)

Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Keep in mind that doInBackground() happens asynchronously, on separate thread. It may take some time to complete, while everything else seems to keep on going.
Glad it worked!