我无法访问“活动”中的“操作”栏

时间:2017-01-21 07:00:02

标签: android

我在访问我的一个Android活动中的操作栏并遇到错误时遇到问题。请找到以下代码。请帮我解决问题,让我知道我犯了什么错误。谢谢你的帮助!!!

错误讯息:

java.lang.RuntimeException: Unable to start activity   ComponentInfo{blueman.vetri.com.materialtest/blueman.vetri.com.materialtest.ui.Task1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference

1)活动

public class Task1 extends AppCompatActivity {

private Toolbar toolbar1;
private Button leave_your_bed;
private Button find_a_place;
private Button opening;
private Button practice;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(blueman.vetri.com.materialtest.R.layout.activity_task1);
    leave_your_bed=(Button)findViewById(blueman.vetri.com.materialtest.R.id.leave_your_bed);
    find_a_place=(Button)findViewById(blueman.vetri.com.materialtest.R.id.find_a_place);
    opening=(Button)findViewById(blueman.vetri.com.materialtest.R.id.opening);
    practice=(Button)findViewById(blueman.vetri.com.materialtest.R.id.practice);

    leave_your_bed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(getApplicationContext(), LeaveYourBed.class);
            startActivity(i);
        }
    });
    find_a_place.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(getApplicationContext(), FindPlace.class);
            startActivity(i);

        }
    });
    opening.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(getApplicationContext(), Opening.class);
            startActivity(i);
        }
    });
    practice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(getApplicationContext(), Practice.class);
            startActivity(i);
        }
    });

    toolbar1 = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar1);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}

2)app_bar xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:background="@color/colorPrimary"
  android:theme="@style/MyCustomToolBarTheme"
>
</android.support.v7.widget.Toolbar>

3)Style.xml文件:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MyCustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorPrimary">#FFF</item>
    <item name="android:textColorSecondary">#000</item>
</style>

</resources>

1 个答案:

答案 0 :(得分:0)

移动代码,将ActionBar设置在super.onCreate下方,并添加以下lint检查:

toolbar1 = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar1);

if (getSupportActionBar() != null) {
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

将以下内容添加到app_bar.xml文件中:

android:id="@+id/app_bar"

其中一个解决方案应该修复NPE。