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 –