当用户点击分享按钮时,这是我的代码。
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login :
loginToTwitter();
break;
case R.id.btn_share:
status = "I'm riding at taxi name: " + operator.getText().toString() + "\n" + "With the plate number of: " + plate.getText().toString() + "\n" +
shareEditText.getText().toString();
if(status.trim().length() > 0) {
new updateTwitterStatus().execute(status);
} else {
Toast.makeText(this, "Message is empty!!", Toast.LENGTH_SHORT).show();
}
break;
}
}
class updateTwitterStatus extends AsyncTask<String, String, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity2Activity.this);
pDialog.setMessage("Posting to Twitter...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... params) {
String status = params[0];
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(consumerKey);
builder.setOAuthConsumerSecret(consumerSecret);
String access_token = sharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
String acces_token_secret = sharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token, acces_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
StatusUpdate statusUpdate = new StatusUpdate(status);
twitter4j.Status response = twitter.updateStatus(statusUpdate);
} catch (TwitterException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT);
shareEditText.setText("");
}
}
当成功发送内容消息时,我在OnPostExecute中分配的Toast不起作用。即使推文没有发送吐司也没有显示。我不知道为什么。我无法确定我的推文是否被推文。但是当我尝试它时,它成功发了推文,但它并没有显示吐司。
答案 0 :(得分:4)
你应该使用show()
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
并且为了更好的编码,使用上下文变量或activityName.this而不是getApplicationContext()
答案 1 :(得分:2)
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT);
Toast未显示,因为您尚未调用show()
方法。
喜欢:
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();