0
中的操作栏我正在面对访问我的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>
我在XML中看不到任何具有app_bar的'id' –
谢谢。但是即使我添加了“id”,我仍然无法访问操作栏 –
您是否在工作中包含了工具栏xml –