2017-04-13 56 views
-1

我的自定義工具欄在toolbar_main.xml如下應用程序試圖通過延長

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <TextView 
     android:id="@+id/app_name" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:textColor="@color/white" 
     android:textSize="40dp" 
     /> 
</LinearLayout> 
</android.support.v7.widget.Toolbar> 

並從activity_main.xml

<include 
     layout="@layout/toolbar_main" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

稱爲在我MainActivity.OnCreate()我設定的描述自定義活動重複使用工具欄時崩潰工具欄作爲操作欄並執行一些操作

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
getSupportActionBar().setDisplayShowHomeEnabled(true); 
getSupportActionBar().setLogo(R.drawable.ic_logo_linkshare); 
getSupportActionBar().setDisplayUseLogoEnabled(true); 
getSupportActionBar().setDisplayShowTitleEnabled(false); 

Typeface ralewaySemiBold = Typeface.createFromAsset(getAssets(), "Raleway-SemiBold.ttf"); 
Typeface ralewayItalic = Typeface.createFromAsset(getAssets(), "Raleway-Italic.ttf"); 
String text1 = "link"; 
String text2 = "share"; 
Spannable s = new SpannableString(text1+text2); 
s.setSpan(new CustomTypefaceSpan(ralewaySemiBold), 0, text1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
s.setSpan(new CustomTypefaceSpan(ralewayItalic), text1.length(), text1.length()+text2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
TextView lettering = (TextView) findViewById(R.id.app_name); 
lettering.setText(s); 

當我有MainActivity我下面

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_main, menu); 
     return true; } 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      [...] 


     } 
} 

一個自定義菜單(這是不同於我想在其他活動中使用的菜單)因爲我想顯示該工具欄(帶有徽標和自定義標題)在我所有的活動(但分開處理菜單)我想創建一個自定義的活動,並隨後擴大它,無論我需要它。

public class MenuActivity extends AppCompatActivity { 

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

     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setLogo(R.drawable.ic_logo_linkshare); 
     getSupportActionBar().setDisplayUseLogoEnabled(true); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 

     Typeface ralewaySemiBold = Typeface.createFromAsset(getAssets(), "Raleway-SemiBold.ttf"); 
     Typeface ralewayItalic = Typeface.createFromAsset(getAssets(), "Raleway-Italic.ttf"); 

     String text1 = "link"; 
     String text2 = "share"; 

     Spannable s = new SpannableString(text1+text2); 
     s.setSpan(new CustomTypefaceSpan(ralewaySemiBold), 0, text1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
     s.setSpan(new CustomTypefaceSpan(ralewayItalic), text1.length(), text1.length()+text2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 

     TextView lettering = (TextView) findViewById(R.id.app_name); 
     lettering.setText(s); 
    } 
} 

基本上從MainActivity移動設置代碼,而是當我啓動的應用程序擴展它

public class MainActivity extends MenuActivity

在發生碰撞這多少結果,沒有任何錯誤或警告在logcat的

我做的東西是不應該的?

--Edit1--

我發現,如果我添加

<include 
layout="@layout/toolbar_main" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"/> 

activity_menu-xml即:中MenuActivity的XML,應用程序不會崩潰,但空的工具欄顯示

--Edit2--

> FATAL EXCEPTION: main 
Process: com.dima.polimi.massimobortolamei.linkshare, PID: 17736 
Theme: themes:{} 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dima.polimi.massimobortolamei.linkshare/com.dima.polimi.massimobortolamei.linkshare.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5461) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference 
    at com.dima.polimi.massimobortolamei.linkshare.activities.MenuActivity.onCreate(MenuActivity.java:23) 
    at com.dima.polimi.massimobortolamei.linkshare.activities.MainActivity.onCreate(MainActivity.java:94) 
    at android.app.Activity.performCreate(Activity.java:6251) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)  
    at android.app.ActivityThread.-wrap11(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5461)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

您可以發佈從崩潰的logcat的? –

+0

不幸的是,Logcat中沒有錯誤!它只是崩潰!請參閱編輯 – MaX

+0

如果應用程序崩潰,應該有一個logcat。檢查您的過濾器 –

回答

0

你可能想給的要olbar包含一個ID。

<include 
    android:id="@+id/toolbar" 
    layout="@layout/toolbar_main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

而且你似乎缺少在MenuActivity工具欄設置,這也解釋了NullPointerException

setContentView(R.layout.activity_menu); 

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

// Find the view from the toolbar 
TextView lettering = (TextView) toolbar.findViewById(R.id.app_name); 

注:一個好的想法是做一個protected void setupToolbar()方法,那麼你可以隱藏掉從MainActivity通過繼承。

public abstract class ToolbarActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(getLayoutRes()); 
     setupToolbar(); 
    } 

    protected abstract int getLayoutRes(); 

    protected void setupToolbar() { 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // Find the view from the toolbar 
     TextView lettering = (TextView) toolbar.findViewById(R.id.app_name); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setLogo(R.drawable.ic_logo_linkshare); 
     getSupportActionBar().setDisplayUseLogoEnabled(true); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 

     Typeface ralewaySemiBold = Typeface.createFromAsset(getAssets(), "Raleway-SemiBold.ttf"); 
     Typeface ralewayItalic = Typeface.createFromAsset(getAssets(), "Raleway-Italic.ttf"); 

     String text1 = "link"; 
     String text2 = "share"; 

     Spannable s = new SpannableString(text1+text2); 
     s.setSpan(new CustomTypefaceSpan(ralewaySemiBold), 0, text1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
     s.setSpan(new CustomTypefaceSpan(ralewayItalic), text1.length(), text1.length()+text2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 

     lettering.setText(s); 
    } 
} 

public class MainActivity extends ToolbarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected int getLayoutRes() { 
     return R.layout.activity_main; 
    } 
+0

我已經有它在'toolbar_main宣佈工具欄的ID。xml',我應該給一個包含ID嗎?我在'MenuActivity'中添加了工具欄設置,但是同樣的錯誤。我應該在「activity_menu」中加入工具欄嗎? – MaX

+0

複製ID不會造成傷害。一般規則:無論「setContentView」使用什麼,都需要工具欄 –

+0

好吧,我已經複製了ID並將工具欄包含在'MenuActivity'的佈局中。該應用程序不再崩潰,但現在工具欄是空的 – MaX