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

Resources » Forums » Android - Learning Android book > (Solved) Why is my Toast not showing Ex 6-5
May 26, 2011 5:09:32 PM PDT (2 years ago). Seen 1,571 times. 5 replies.
Photo Tom Farrell
None
Member since May 26, 2011
Forum Posts: 2
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).
May 27, 2011 8:48:48 AM PDT (2 years ago)
Photo Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Forum Posts: 228
:) That's why @Override helps.
March 5, 2012 2:32:33 AM PST (one year ago)
Photo Juan Menendez
Home
Member since Mar 5, 2012
Forum Posts: 2
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");
}
March 5, 2012 9:02:34 PM PST (one year ago)
Photo Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Forum Posts: 228
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?
March 6, 2012 1:13:14 AM PST (one year ago)
Photo Juan Menendez
Home
Member since Mar 5, 2012
Forum Posts: 2
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 :)
March 6, 2012 8:32:01 PM PST (one year ago)
Photo Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Forum Posts: 228
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!