Reputation: 143
Well, my app crashes when I click the button I linked to a new Activity
. Here's all my code:
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>
As you can see in MyActivity
I start MyActivity2
. The switching between those two is working flawlessly, but when I click the button in MyActivity
to switch to the third activity it crashes the app? :/
EDIT:
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
EDIT:
Manifest:
<?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>
Upvotes: 1
Views: 2881
Reputation: 19949
This line of MyActivity3
m.setTypeface(typeface);
throws NullPointerException because m
is null. In its turn it's set with null at this line:
Button m = (Button) findViewById(R.id.button3);
because it cannot be found by R.id.button3
due to its absence in the activity_my3.xml
file. Declare the button in the file.
Upvotes: 2