Android 单击“选项”时不显示 Snackbar

Android Snackbar not appearing when I click Option Item

我试图在每次单击我的选项时显示一个 Snackbar,但是当我这样做时,我的应用程序崩溃了。有谁知道这里出了什么问题以及如何解决这个问题?

java

public class MainActivity extends AppCompatActivity {
    private static CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Snackbar.make(coordinatorLayout, "Single Line SnackBar Demo.", Snackbar.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Logcat

04-17 09:22:58.260 20108-20108/com.apptacularapps.snackbar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.apptacularapps.snackbar, PID: 20108
                                                                             java.lang.NullPointerException
                                                                                 at android.support.design.widget.Snackbar.<init>(Snackbar.java:183)
                                                                                 at android.support.design.widget.Snackbar.make(Snackbar.java:215)
                                                                                 at com.apptacularapps.snackbar.MainActivity.onOptionsItemSelected(MainActivity.java:49)
                                                                                 at android.app.Activity.onMenuItemSelected(Activity.java:2600)
                                                                                 at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
                                                                                 at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:179)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                 at android.support.v7.app.ToolbarActionBar.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                 at android.support.v7.widget.Toolbar.onMenuItemClick(Toolbar.java:169)
                                                                                 at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                                                                                 at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
                                                                                 at android.widget.AdapterView.performItemClick(AdapterView.java:299)
                                                                                 at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
                                                                                 at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
                                                                                 at android.widget.AbsListView.run(AbsListView.java:3645)
                                                                                 at android.os.Handler.handleCallback(Handler.java:733)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:136)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                 at dalvik.system.NativeStart.main(Native Method)

可能是您的 coordinatorlayout 未初始化或未在 class.That 中定义可能是导致您收到 空指针异常 的原因。

您没有初始化协调器布局。像这样初始化它:

coordinatorLayout = (CoordinatorLayout) findViewById(R.id.yourCoordinatorLayoutId);

初始化CoordinatorLayout或传递CoordinatorLayout

的任何子视图

coordinatorLayout = (CoordinatorLayout) findViewById(R.id.yourCoordinatorLayoutId);

TextView textView = (TextView) findViewById(R.id.yourTextViewID);

您的 TexView 位于您的 CoordinatorLayout 中,然后将此视图传递给您的 Snackbar,例如:

Snackbar.make(textView, "Single Line SnackBar Demo.", Snackbar.LENGTH_SHORT).show();