こんにちは、いくつかのコードに問題があります。1 つのアクティビティから 3 つの整数を取得し、それらの数値を使用してキャンバスに円を描画しようとしています。これが私の2番目の活動です:
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
int x = b.getInt("X");
int y = b.getInt("Y");
int r = b.getInt("R");
Canvas canvas = new Canvas();
View canView = new View(this);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)canView.getLayoutParams();
params.height = 300;
params.width = 400;
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Rect rec = new Rect();
rec.set(0,0, 400, 300);
Paint paint = new Paint();
Paint paint2 = new Paint();
paint2.setColor(Color.BLUE);
paint.setColor(Color.BLACK);
canvas.drawRect(rec, paint);
canvas.drawCircle(x, y, r, paint2);
canView.setLayoutParams(params);
canView.draw(canvas);
setContentView(canView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity2, menu);
return true;
}
}
このアクティビティを開始すると、Null Pointer 例外が発生します。これが私のlogcatです:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mrsai.exampleactivity/com.mrsai.exampleactivity.Activity2}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
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:1008)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mrsai.exampleactivity.Activity2.onCreate(Activity2.java:30)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
... 11 more
助けていただければ幸いです:)