使用Material Design将程序从Launcher活动转移到另一个活动时,应用程序崩溃

时间:2015-09-21 04:40:18

标签: android exception nullpointerexception material-design

我正在尝试在我现有的一个项目上实现材质设计,主题完全适用于启动器活动但是当我导航到另一个活动时它会崩溃应用程序。

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.hsclock" >

    <uses-feature android:name="android.hardware.camera" />

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/MyMaterialTheme" >
        <activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity
            android:name=".CategoryActivity"
            android:label="@string/title_activity_category"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.mycompany.hsclock.MainActivity" />
        </activity>
        <activity
            android:name=".SwipeViews.SwipeView"
            android:label="@string/title_activity_swipe_view" >
        </activity>
        <activity
            android:name=".SaveOnShare.SaveShare"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Styles.xml

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base" >
        <item name = "android:icon">@drawable/ic_launcher</item></style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
        <item name="android:colorAccent">@color/accent</item>

    </style>

values-v21,styles.xml

<resources>

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>

</resources>

主要活动

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        initCategoryList();
        createCategoryPopup();
        populateCategoryList();
    }

调用第二个活动的方法

private void initCategoryList()
    {

        listView = (ListView) findViewById(R.id.list);
        mCategoryAdapter=new CategoryAdapter(this,mCategoryItems);

        // Assign adapter to ListView
        listView.setAdapter(mCategoryAdapter);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent,
                                           View view, int position, long id) {
                onListItemSelect(position);
                return true;
            }
        });

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                if (mActionMode == null) {
                    // ListView Clicked item index
                    int itemPosition = position;

                    // ListView Clicked item value
                    CategoryItem itemValue = (CategoryItem) parent.getItemAtPosition(position);

                    // Show Alert
                    Toast.makeText(getApplicationContext(),
                            "Position :" + itemPosition + "  ListItem : " + itemValue.name, Toast.LENGTH_LONG)
                            .show();


                    Intent categoryintent = new Intent(getBaseContext(), CategoryActivity.class);

                    if (itemValue != null) {
                        categoryintent.putExtra("cat_id", itemValue.id);
                        categoryintent.putExtra("cat_name", itemValue.name);
                    }
                    startActivity(categoryintent);
                } else
                    // add or remove selection for current list item
                    onListItemSelect(position);

            }

        });


    }

第二项活动

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        // Catching intent of the selected category
        Intent data = getIntent();
        if(data !=  null){
            catId = data.getIntExtra("cat_id", -1);
            catName = data.getStringExtra("cat_name");

        }

第二个活动xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"

    android:orientation="vertical"
    tools:context=".MainActivity">


    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>

    <ListView
        android:id="@+id/list"
        android:layout_weight="10"
        android:layout_height="100dp"
        android:layout_width="match_parent">
    </ListView>

</LinearLayout>

错误记录

Caused by: java.lang.NullPointerException
            at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:94)
            at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:87)
            at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:79)
            at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
            at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
            at com.mycompany.hsclock.CategoryActivity.onCreate(CategoryActivity.java:71)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

试试这个 -

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);

    View myView = findViewById(R.id.toolbar);
    Toolbar mtoolbar = (Toolbar) myView.findViewById(R.id.toolbar);
    setSupportActionBar(mtoolbar);

    // Catching intent of the selected category
    Intent data = getIntent();
    if(data !=  null){
        catId = data.getIntExtra("cat_id", -1);
        catName = data.getStringExtra("cat_name");

    }
}

答案 1 :(得分:0)

其实我在setcontentview之前设置工具栏......

修改后的代码 -

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Catching intent of the selected category
            Intent data = getIntent();

            if(data !=  null){
                catId = data.getIntExtra("cat_id", -1);
                catName = data.getStringExtra("cat_name");

            }

            setContentView(R.layout.activity_category);

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

            initCategoryList();
            populateImageList();

           ...