Toast 不工作 (Android)
Toast not working (Android)
当我运行这个方法
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
从另一个线程,应用程序只能从 onCreate 开始工作,但是
DebugText.setText(txt);
到处都能正常工作...还有谁可以提供帮助?
public void screenMessage(final String txt) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//DebugText.setText(txt);
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
}
});
}
我的解决方案如下(对我来说它适用于任何地方):
public static void showToast(final Context ctx, final String msg, int type) {
if (ctx == null || TextUtils.isEmpty(msg))
return;
final int toastType = type == Toast.LENGTH_LONG ? Toast.LENGTH_LONG
: Toast.LENGTH_SHORT;
if (Looper.myLooper() == Looper.getMainLooper()) {
Toast.makeText(ctx, msg, toastType).show();
} else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, msg, toastType).show();
}
});
}
}
我想你错过了呼叫开始试试这个
public void screenMessage(final String txt) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//DebugText.setText(txt);
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
}
}).start();
当我运行这个方法
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
从另一个线程,应用程序只能从 onCreate 开始工作,但是
DebugText.setText(txt);
到处都能正常工作...还有谁可以提供帮助?
public void screenMessage(final String txt) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//DebugText.setText(txt);
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
}
});
}
我的解决方案如下(对我来说它适用于任何地方):
public static void showToast(final Context ctx, final String msg, int type) {
if (ctx == null || TextUtils.isEmpty(msg))
return;
final int toastType = type == Toast.LENGTH_LONG ? Toast.LENGTH_LONG
: Toast.LENGTH_SHORT;
if (Looper.myLooper() == Looper.getMainLooper()) {
Toast.makeText(ctx, msg, toastType).show();
} else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, msg, toastType).show();
}
});
}
}
我想你错过了呼叫开始试试这个
public void screenMessage(final String txt) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//DebugText.setText(txt);
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
}
}).start();