I face a problem when start my app and I want to know what is the problem exactly.
I get this in log cat:
09-08 12:40:35.690: W/dalvikvm(1382): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-08 12:40:35.740: E/AndroidRuntime(1382): FATAL EXCEPTION: main
09-08 12:40:35.740: E/AndroidRuntime(1382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yahya.myfirstapp/com.yahya.myfirstapp.Main}: java.lang.NullPointerException
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.os.Looper.loop(Looper.java:123)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-08 12:40:35.740: E/AndroidRuntime(1382): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 12:40:35.740: E/AndroidRuntime(1382): at java.lang.reflect.Method.invoke(Method.java:507)
09-08 12:40:35.740: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-08 12:40:35.740: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-08 12:40:35.740: E/AndroidRuntime(1382): at dalvik.system.NativeStart.main(Native Method)
09-08 12:40:35.740: E/AndroidRuntime(1382): Caused by: java.lang.NullPointerException
09-08 12:40:35.740: E/AndroidRuntime(1382): at com.yahya.myfirstapp.Main.onCreate(Main.java:19)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-08 12:40:35.740: E/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-08 12:40:35.740: E/AndroidRuntime(1382): ... 11 more
09-08 12:40:41.260: I/Process(1382): Sending signal. PID: 1382 SIG: 9
and this is Main.java
package com.yahya.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(android.R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
and this Second.java
package com.yahya.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/welcome" />
</RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/test_secondactivity" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/welcome" />
</LinearLayout>
Correct this line:
Button b = (Button) findViewById(android.R.id.button1);
to be:
Button b = (Button) findViewById(R.id.button1);
android.R
is used to access built in system resources imbeded in the Android OS and there is no resource called button1
确保在androidmanifest.xml中标识了您的类
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.