嗨即時通訊有問題的一些代碼。我試圖從一個活動獲得3個整數,並使用這些數字在畫布上繪製一個圓。這裏是我的第二個活動:空指針異常
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;
}
}
當開始這個活動,我得到一個空指針異常。繼承人我的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
編號非常感謝任何幫助:)
30是哪裏? – 323go
Activity2中哪行是'30'? – SudoRahul
Activity2.java:30第30行? – t0s