我制作了一个 android 应用程序,其中一个活动打开了另一个。打开的是:
package com.tendariuspro.oz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Second extends Activity {
Button btnYes;
Button btnNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
btnYes = (Button) findViewById (R.id.btnYesTwo);
btnNo = (Button) findViewById (R.id.btnNoTwo);
btnYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Class<?> cc = Class.forName("com.tendariuspro.oz.Tenis");
Intent ii = new Intent(Second.this, cc);
startActivity(ii);
finish();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Class<?> c = Class.forName("com.tendariuspro.oz.Baschet");
Intent i = new Intent(Second.this, c);
startActivity(i);
finish();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
btnYes 打开的是:
package com.tendariuspro.oz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Baschet extends Activity{
Button btnYes;
Button btnNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.baschet);
btnYes = (Button) findViewById (R.id.btnYesOne);
btnNo = (Button) findViewById (R.id.btnNoOne);
btnYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Class<?> cc = Class.forName("com.tendariuspro.oz.Razvan");
Intent ii = new Intent(Baschet.this, cc);
startActivity(ii);
finish();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Class<?> c = Class.forName("com.tendariuspro.oz.Rares");
Intent i = new Intent(Baschet.this, c);
startActivity(i);
finish();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
注意:btnNo 开启的活动与此类似!
但是当我按下 btnNo 或 btnYes 时,它在两个活动中的第 22 行(btnYes)给了我一个 NullPointerException !
你能帮我吗!
这是LogCat:
06-16 07:59:09.395: E/AndroidRuntime(1465): FATAL EXCEPTION: main
06-16 07:59:09.395: E/AndroidRuntime(1465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tendariuspro.oz/com.tendariuspro.oz.Baschet}: java.lang.NullPointerException
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.os.Looper.loop(Looper.java:137)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-16 07:59:09.395: E/AndroidRuntime(1465): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 07:59:09.395: E/AndroidRuntime(1465): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 07:59:09.395: E/AndroidRuntime(1465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-16 07:59:09.395: E/AndroidRuntime(1465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-16 07:59:09.395: E/AndroidRuntime(1465): at dalvik.system.NativeStart.main(Native Method)
06-16 07:59:09.395: E/AndroidRuntime(1465): Caused by: java.lang.NullPointerException
06-16 07:59:09.395: E/AndroidRuntime(1465): at com.tendariuspro.oz.Baschet.onCreate(Baschet.java:22)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.Activity.performCreate(Activity.java:5104)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-16 07:59:09.395: E/AndroidRuntime(1465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-16 07:59:09.395: E/AndroidRuntime(1465): ... 11 more