Android启动新的活动错误

时间:2015-10-15 11:51:55

标签: android

我需要一些代码的帮助,由于某些原因我无法在“onNavigationDrawerItemSelected”中启动一个新的Activity

这是我的主要活动:

package com.spadramatico;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
        implements NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mToolbar);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.fragment_drawer);

        // Set up the drawer.
        mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();



        if(position == 1) {

            Intent i=new Intent(MainActivity.this, spadramaticoActivity.class);
            startActivity(i);

        }


    }



    @Override
    public void onBackPressed() {
        if (mNavigationDrawerFragment.isDrawerOpen())
            mNavigationDrawerFragment.closeDrawer();
        else
            super.onBackPressed();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

这是我的spadramaticoActivity.class

package com.spadramatico;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


/**
 * Created by IvoMi on 15/10/2015.
 */
public class spadramaticoActivity extends AppCompatActivity {

    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meu_spadramatico_fragment);

        toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

这是错误:

10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime: FATAL EXCEPTION: main
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime: Process: com.spadramatico, PID: 2165
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spadramatico/com.spadramatico.spadramaticoFragment}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:204)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at com.spadramatico.spadramaticoFragment.onCreate(spadramaticoFragment.java:27)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-15 11:36:03.816 2165-2165/com.spadramatico E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我看到其他stackoverflow问题,我明白我需要在清单中拥有该活动..

这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spadramatico" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >


        <activity
            android:name=".MainActivity"
            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=".spadramaticoActivity"
            android:label="@string/app_name"
            android:parentActivityName=".MainActivity">

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.spadramatico.MainActivity">

            </meta-data>
        </activity>

    </application>

</manifest>

有人能告诉我我做错了什么,我该如何解决? 谢谢你们!

1 个答案:

答案 0 :(得分:1)

对我来说,似乎你在onCreate中夸大了错误的布局,即setContentView(R.layout.meu_spadramatico_fragment)。您正在膨胀片段布局,而不是您想要用于活动的片段布局。我想在这个片段布局中没有工具栏,因此工具栏为空。这就是错误所说的。