我已经创建了一个Android应用程序,其中aactivity打开另一个。打开的是:
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行给出一个NullPointerException(btnYes) 在这两个活动中!
你能帮帮我吗?
这是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
答案 0 :(得分:0)
btnYes为空。
检查res / layut / baschet.xml以查看此按钮是否有id btnYesOne。