私は、アクティビティが別のものを開く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