尝试通过扩展自定义活动重用工具栏时应用崩溃

时间:2017-04-13 15:24:20

标签: android nullpointerexception android-toolbar

我的自定义工具栏在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()) {
            [...]


        }
}

由于我想在我的所有活动中显示这个工具栏(带有徽标和自定义标题)(但是单独处理菜单),我考虑创建一个自定义Activity,然后将其扩展到我需要的任何地方。

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) 

1 个答案:

答案 0 :(得分:1)

您可能希望为工具栏添加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;
    }