工具栏导致应用崩溃

时间:2015-10-26 18:14:44

标签: android layout

希望你能提供帮助。

我正在完成一个教程,但其中一条线正在使应用程序崩溃,如果我将其留在应用程序崩溃中但如果我把它拿出来运行应用程序,问题是,我需要应用程序中的菜单栏

这是

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

这是onCreate方法的完整代码

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

    mListView = (ListView) findViewById(R.id.reminders_list_view);
    mListView.setDivider(null);
    mDbAdapter = new RemindersDbAdapter(this);
    mDbAdapter.open();
    Cursor cursor = mDbAdapter.fetchAllReminders();

    String[] from = new String[]{
            RemindersDbAdapter.COL_CONTENT
    };

    int[] to = new int[]{
            R.id.row_text
    };
    mCursorAdapter = new RemindersSimpleCursorAdapter(RemindersActivity.this,R.layout.reminders_row,cursor,from,to,0);

    mListView.setAdapter(mCursorAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_reminders, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_new:
        //create new Reminder
            Log.d(getLocalClassName(), "create new Reminder");
            return true;
        case R.id.action_exit:
            finish();
            return true;
        default:
            return false;
    }
}  

这是xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".RemindersActivity">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_reminders"/>

有人可以告诉我我做错了什么。我还没有充分理解android还没有找出导致这个问题的原因,但希望有人之前遇到过这个问题,或者知道该怎么做才能提供帮助。我真的想继续学习教程

谢谢你,希望你能理解这个问题。

1 个答案:

答案 0 :(得分:1)

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

下面

setContentView(R.layout.activity_reminders);

E.g:

setContentView(R.layout.activity_reminders);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
  

findViewById()返回一个View(如果它存在于您提供的布局中)   在setContentView()中,否则返回null,那是什么   发生在你身上

     

如果你setContentView(R.layout.activity_reminders);   然后调用findViewById(R.id.toolbar);它会返回一个View   是你的布局,但如果你调用findViewById(R.id.toolbar);之前   的setContentView(R.layout.activity_reminders);它将返回null   您的活动没有附加视图。