我的自定義工具欄在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)
您可以發佈從崩潰的logcat的? –
不幸的是,Logcat中沒有錯誤!它只是崩潰!請參閱編輯 – MaX
如果應用程序崩潰,應該有一個logcat。檢查您的過濾器 –