Android 切换活动时应用程序崩溃

Android 切换活动时应用程序崩溃,android,android-activity,nullpointerexception,Android,Android Activity,Nullpointerexception,嗯,当我点击链接到新活动的按钮时,我的应用程序崩溃了。以下是我所有的代码: MyActivity: public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_m

嗯,当我点击链接到新
活动的按钮时,我的应用程序崩溃了。以下是我所有的代码:

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);

        }
}
<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>
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);
    }
}
<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>
<?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>
活动\u my.xml

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);

        }
}
<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>
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);
    }
}
<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>
<?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>
活动\u my3.xml

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);

        }
}
<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>
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);
    }
}
<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>
<?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>
编辑:

清单

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);

        }
}
<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>
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);
    }
}
<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>
<?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>

这一行的
MyActivity3

m.setTypeface(typeface);
抛出NullPointerException,因为
m
为null。在这一行,它被设置为null:

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

因为
R.id.button3
无法找到它,因为它在
活动\u my3.xml
文件中不存在。在文件中声明按钮。

当logcat崩溃时,它有什么错误?您是否正确配置了清单文件?如果是的话,把它贴在这里,这样我就可以看到它了logcat是不可能贴出来的,它一直在进行各种过程。但我真的很蠢我根本不知道名单上有什么。。。只需一秒钟,“它会继续处理各种进程”——如果崩溃,那么您正在寻找Java堆栈跟踪。这是一本书。