无法在 Android 中显示 Toast

Cannot show a Toast in Android

我想在调用 toast() 时显示 Toast,但它没有显示 Toast。

这是我的代码:

void callcalltoast() {
    ...
    calltoast();
    ...
}
void calltoast() {
    ...
    toast();
    ...
}
void toast() {
    Log.i("LOG", "this is toast");
    Toast.makeText(getBaseContext(),"this is toast",
               Toast.LENGTH_SHORT).show();
}

我在另外两个函数中调用 toast:

先调用 callcalltoast() 然后 calltoast() 然后调用 toast()

这是日志:

08-04 14:47:34.390: I/LOG(1796): this is toast
08-04 14:47:34.430: I/Choreographer(1796): Skipped 90 frames!  The application may be doing too much work on its main thread.
08-04 14:47:34.451: W/InputMethodManagerService(285): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41a1c750 attribute=null, token = android.os.BinderProxy@419ff990

我用这个测试过:

void toast() {
    Log.i("LOG", "this is toast");
    Toast.makeText(getApplicationContext(),"this is toast",
               Toast.LENGTH_SHORT).show();
}

这个

void toast() {
    Log.i("LOG", "this is toast");
    Toast.makeText(this,"this is toast",
               Toast.LENGTH_SHORT).show();
}

void toast() {
    Log.i("LOG", "this is toast");
    Toast.makeText(MainActivity.this,"this is toast",
               Toast.LENGTH_SHORT).show();
}

但它没有显示 Toast。

请勿拨打getBaseContext()。如果您在 Activity 中,只需将 this 作为上下文传递,或者您可以使用 getApplicationContext() 来获取一个

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    callcall();


}
private void callcall() {
    .....
    calltoast();
    .....
}

private void calltoast() {
    .....
    toast();
    .....
}

private void toast(){
    Toast.makeText(getApplicationContext(),"message",Toast.LENGTH_SHORT).show();
}

}

使用此代码:

public enum Toaster {
    INSTANCE;

    private final Handler handler = new Handler(Looper.getMainLooper());

    public void postMessage(Context context, final String message) {
        handler.post(
            new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, message, Toast.LENGTH_SHORT)
                        .show();
                }
            }
        );
    }
}

然后是这个:

Toaster.INSTANCE.postMessage(this, "this is toast");

它应该在任何线程中都以这种方式工作。通常我只有一个 ApplicationContext 的单例持有者,我会将其作为参数提供。

你可以做的是将 Context 传递给如下函数

void toast(Context context) {
   Log.i("LOG", "this is toast");
   Toast.makeText(context,"this is toast",
           Toast.LENGTH_SHORT).show();
}

调用这个函数只需写

这是你在 activity

中调用它的时候
toast(Your_Activity_Name.this); 

在你的情况下会像下面这样

 toast(MainActivity .this);

如果你在片段中使用它,那么

 toast(getActivity());

"Skipped 90 frames!" the possibility can be is that you are testing your app on emulator if yes then don't worry about that. and if you are testing on real device then this log is not because of showing but because of your other code.

使用此代码

你可以在 Activity 创建时声明全局变量 Context context;context=MainActivity.this; 然后当你调用 toast 时你可以使用像 toast(context);

这样的上下文
public class MainActivity extends ActionBarActivity {

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        context=MainActivity.this;

        callcalltoast();
        ...
    }
    void callcalltoast() {
    ...
    calltoast();
    ...
    }
    void calltoast() {
        ...
        toast(context);
        ...
    }
    void toast(Context context) {
        Log.i("LOG", "this is toast");
        Toast.makeText(context,"this is toast",
                   Toast.LENGTH_SHORT).show();
    }
}