切换活动时应用程序崩溃

时间:2014-08-16 23:02:40

标签: android android-activity nullpointerexception

好吧,当我点击链接到新Activity的按钮时,我的应用会崩溃。这是我的所有代码:

MyActivity

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Button n = (Button) findViewById(R.id.button);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
        n.setText("I AM BRD.");
        n.setTypeface(typeface);
    }

    public void openNewActivity(View view) {
    // Do something in response to button
        Intent intent = new Intent(this, MyActivity2.class);
        startActivity(intent);
        overridePendingTransition(R.anim.animation, R.anim.animation2);
    }

    public void openNewActivity2(View view) {
    // Do something in response to button
        Intent intent = new Intent(this, MyActivity3.class);
        startActivity(intent);

        }
}

Activity_my.xml

<RelativeLayout 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"
    tools:context=".MyActivity"
    android:id="@+id/button2"
    android:background="#ff3498db">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I AM BRD."
        android:id="@+id/button"
        android:textColor="#ffffffff"
        android:textSize="72sp"
        android:background="#00000000"
        android:onClick="openNewActivity"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="openNewActivity2"
        android:background="@drawable/sideicon" />
</RelativeLayout>

MyActivity3

public class MyActivity3 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my3);
        Button m = (Button) findViewById(R.id.button3);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
        m.setTypeface(typeface);
    }
}

Activity_my3.xml

<RelativeLayout 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"
    tools:context=".MyActivity2"
    android:background="#2ECC71">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/cat"
        android:id="@+id/textViewcat"
        android:textColor="#ffffffff"
        android:textSize="72sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

正如您在MyActivity中所看到的,我开始MyActivity2。这两者之间的切换工作完美无缺,但当我点击MyActivity中的按钮切换到第三个活动时,它会崩溃应用程序? :/

编辑:

08-17 01:13:43.844  25021-25021/com.MR.brd E/MMUMapper﹕ fail to register MVA,   unsupported format(0x5)
08-17 01:13:48.499  25021-25021/com.MR.brd D/VelocityTracker﹕ Couldn't open '/dev/touch'   (No such file or directory)
08-17 01:13:48.499  25021-25021/com.MR.brd D/VelocityTracker﹕ tpd read x fail: Bad file number
08-17 01:13:48.499  25021-25021/com.MR.brd D/VelocityTracker﹕ tpd read y fail: Bad file number
08-17 01:13:48.575  25021-25021/com.MR.brd V/Provider/Setting﹕ invalidate [system]: current 2719 != cached 0
08-17 01:13:48.579  25021-25021/com.MR.brd V/Provider/Setting﹕ from db cache, name = sound_effects_enabled value = 0
08-17 01:13:48.665  25021-25021/com.MR.brd D/AndroidRuntime﹕ Shutting down VM
08-17 01:13:48.665  25021-25021/com.MR.brd W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x410458a8)
08-17 01:13:48.670  25021-25021/com.MR.brd E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.MR.brd/com.MR.brd.MyActivity3}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
        at android.app.ActivityThread.access$600(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5071)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.MR.brd.MyActivity3.onCreate(MyActivity3.java:21)
        at android.app.Activity.performCreate(Activity.java:5247)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
        at android.app.ActivityThread.access$600(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5071)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)
08-17 01:13:51.042  25021-25021/com.MR.brd I/Process﹕ Sending signal. PID: 25021 SIG: 9

编辑:

清单

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/splashScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.MR.brd.MyActivity2"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name="com.MR.brd.MyActivity3"
            android:label="BRD."
            android:screenOrientation="portrait">
            </activity>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

这一行MyActivity3

m.setTypeface(typeface);

抛出 NullPointerException ,因为m null 。反过来,它在此行设置 null

Button m = (Button) findViewById(R.id.button3);

因为R.id.button3文件中缺少activity_my3.xml,因此无法找到{{1}}。声明文件中的按钮。