-3

嗨,我遇到了一些代码问题。我试图从一个活动中获取 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;
}

}

开始此活动时,我得到一个空指针异常。这是我的日志:

    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

我非常感谢任何帮助:)

4

1 回答 1

0

我认为你的 put bundle 函数在字符串中,在 actity2 中以这种方式尝试获取字符串。

String xx = b.getString("X");
int x = Integer.valueOf(xx);
于 2013-03-29T12:19:34.507 回答