2016-07-04 97 views
-1

在我的應用程序上打開新的活動,那次我得到一個下面的錯誤。
android.support.v7.widget.Toolbar.getTitle()'空對象引用

java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle() 

我的XML代碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical"> 
<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/order_detail_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/appcolor" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

</android.support.design.widget.AppBarLayout> 

我的Java代碼:

private Toolbar toolbar; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.order_detail); 
    String orderid = getIntent().getStringExtra("order_detail_toolbar"); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    setTitle("DDIT_Results"); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
} 

任何一個知道的解決方案,請解釋錯誤,並給予解決。

回答

1

你試圖用錯誤的ID來獲得的工具欄。

您設定的ID是

android:id="@+id/order_detail_toolbar"

但試圖讓它在

toolbar = (Toolbar) findViewById(R.id.toolbar);

讓行更改爲toolbar = (Toolbar) findViewById(R.id.order_detail_toolbar);

+0

感謝編號是錯誤的 – vinoth12594

+0

如果您使用的是像Android Studio這樣的IDE,一個好的提示是CMD-點擊該編號以查看它鏈接到了xml中的正確元素。這也是一種很好的導航方法,你可以在方法,類等上做同樣的事情。 – raxelsson

1

編輯

toolbar = (Toolbar) findViewById(R.id.order_detail_toolbar); 
    setSupportActionBar(toolbar); 
    setTitle("DDIT_Results"); 
+0

你已改變? –

+0

Thanx爲您的重播 – vinoth12594

+0

歡迎朋友:) –

1
Toolbar toolbar = (Toolbar) findViewById(R.id.order_detail_toolbar); 
setSupportActionBar(toolbar);  
getSupportActionBar().setTitle("DDIT_Results"); 
+0

這並沒有解決OP所面臨的問題。 – Sufian

相關問題