2017-08-16 29 views
-2

我試圖在RegisterActivity的Action Bar中添加按鈕,但它將我帶出應用程序而沒有崩潰,並且警告告訴我setDisplayHomeAsUpEnabled可能會產生NullPonterException,我遵循Android Developer控制檯中的指令,並且看到很多類似於我的案例,但冷卻沒有實現我的案件的答案的問題,所以我需要一個來幫助我的案件。 這裏是我的代碼:setDisplayHomeAsUpEnabled產生NullPonterException

RegisterActivity:

public class RegisterActivity extends AppCompatActivity { 
private Toolbar mToolbar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register);   

    mToolbar = (Toolbar) findViewById(R.id.register_toolbar); 
    setSupportActionBar(mToolbar); 
    getSupportActionBar().setTitle("Creat Account"); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    } 

} 

我試試這個,但也沒有工作:

if (getSupportActionBar() != null) { 
     getSupportActionBar().setTitle("Creat Account"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
    } 

activity_register.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.mohamed71.lapitchat.RegisterActivity"> 

<include 
    android:id="@+id/register_toolbar" 
    layout="@layout/app_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteX="8dp" 
    tools:layout_editor_absoluteY="0dp" /> 

app_bar_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 

xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/main_app_bar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/colorPrimary" 
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar" 
android:minHeight="?attr/actionBarSize" 
> 

</android.support.v7.widget.Toolbar> 

AndriodMainifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mohamed71.lapitchat"> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<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=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".StartActivity" /> 
    <activity 
     android:name=".RegisterActivity" 
     android:parentActivityName=".StartActivity" /> 
</application> 

enter image description here

+0

的可能的複製[什麼是空指針異常,以及如何解決?(https://stackoverflow.com/questions/218384/什麼 - 是 - 一 - NullPointerException異常和知識-DO-I-FIX-IT) – Zoe

回答

0

您可以嘗試如下:

mToolbar = (Toolbar) findViewById(R.id.register_toolbar); 
setSupportActionBar(mToolbar); 

android.support.v7.app.ActionBar ab = getSupportActionBar(); 
if(ab != null){ 
    ab.setTitle("Creat Account"); 
    ab.setDisplayHomeAsUpEnabled(true); 
    ab.setDisplayShowHomeEnabled(true); 
} 
1

您正在添加錯誤的工具欄ID。試着改變你的代碼

mToolbar = (Toolbar) findViewById(R.id.main_app_bar); 
    setSupportActionBar(mToolbar); 

,而不是

mToolbar = (Toolbar) findViewById(R.id.register_toolbar); 
    setSupportActionBar(mToolbar);