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

时间:2016-04-17 13:31:42

标签: java android android-support-library android-snackbar

我在点击我的选项时试图显示Snackbar但是当我这样做时,我的应用程序崩溃了。有谁知道这里有什么问题,可以采取哪些措施来解决这个问题?

enter image description here

的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$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                 at android.support.v7.widget.Toolbar$1.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$3.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)

3 个答案:

答案 0 :(得分:4)

您没有初始化协调器布局。将其初始化为:

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

答案 1 :(得分:1)

可能是你的coordinatorlayout未在类中初始化或定义。这可能是你得到的空指针异常的原因。

答案 2 :(得分:0)

初始化CoordinatorLayout或传递CoordinatorLayout

的所有子视图

即。

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

OR

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

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

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