-1
經過幾個小時尋找解決方案以查找代碼中的錯誤,我最終在這裏提出了一個問題。我嘗試了所有提供的解決方案,沒錯。也許我的代碼有問題。經過數小時的搜索解決方案後,無法將按鈕添加到操作欄
因此,這些都是我的代碼:
AboutUsActivity.java:
public class AboutUsActivity extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
我AndroidManifest文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.GeneralSettingActivity" android:parentActivityName=".activity.ProfileActivity"/>
<activity android:name=".activity.RegisterActivity" android:parentActivityName=".activity.LoginActivity"/>
<activity android:name=".activity.ResetPasswordActivity" android:parentActivityName=".activity.LoginActivity"/>
<activity android:name=".activity.HomeActivity" />
<activity android:name=".activity.AboutUsActivity"
android:parentActivityName=".activity.HomeActivity"
android:theme="@style/AppTheme.ActionBar"
/>
<activity android:name=".activity.ApplyTutor" android:parentActivityName=".activity.HomeActivity"/>
<activity android:name=".activity.PrivacyPolicyActivity" android:parentActivityName=".activity.HomeActivity"/>
<activity
android:name=".activity.ProfileActivity"
android:label="@string/title_activity_profile"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".activity.EditProfileActivity" android:parentActivityName=".activity.ProfileActivity"/>
<activity android:name=".activity.EarningActivity" android:parentActivityName=".activity.ProfileActivity"/>
<activity android:name=".activity.ScheduleActivity" android:parentActivityName=".activity.ProfileActivity"/>
<activity android:name=".activity.RatingReviewActivity" android:parentActivityName=".activity.ProfileActivity"/>
<activity android:name=".activity.RequestActivity"></activity>
</application>
我styles.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>
<item name="android:windowFullscreen">false</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.ActionBar"/>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
我activity_about_us.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.cqaai.tutorpal.activity.AboutUsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Page name: About Us"/>
</android.support.constraint.ConstraintLayout>
Android的顯示器顯示錯誤:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.cqaai.tutorpal.activity.AboutUsActivity.onCreate(AboutUsActivity.java:32)
您是否指一個導航圖標,點擊後轉到其他活動? – Xenolion