Toast 未显示在发布推文中
Toast is not showing in posting tweet
这是我在用户点击分享按钮时的代码。
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 不起作用。即使推文未发送,敬酒也未显示。我不知道为什么。我无法确定我的推文是否被转发。但是当我尝试它时,它成功地发了推文,但它没有显示吐司。
你应该使用 show()
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
为了更好的编码使用上下文变量或 activityName.this 而不是 getApplicationContext()
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT);
Toast 未显示,因为您尚未调用 show()
方法。
喜欢:
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
这是我在用户点击分享按钮时的代码。
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 不起作用。即使推文未发送,敬酒也未显示。我不知道为什么。我无法确定我的推文是否被转发。但是当我尝试它时,它成功地发了推文,但它没有显示吐司。
你应该使用 show()
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();
为了更好的编码使用上下文变量或 activityName.this 而不是 getApplicationContext()
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT);
Toast 未显示,因为您尚未调用 show()
方法。
喜欢:
Toast.makeText(getApplicationContext(), "Posted to Twitter!", Toast.LENGTH_SHORT).show();