-3

Hi im having trouble with some code. Im trying to get 3 integers from one activity and use those numbers to draw a circle on a canvas. Here is my 2nd activity:

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;
}

}

When starting this activity i get a Null Pointer exception. Heres my 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

Id greatly appreciate any help :)

HighMrSai
  • 97
  • 1
  • 1
  • 9

1 Answers1

0

Your put bundle function is in string i think,Try it like this way in get string in acticity2.

String xx = b.getString("X");
int x = Integer.valueOf(xx);
Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • actually in the first activity i used b.putInt instead of putString. I used parseInt to get the integers from the editText. btw whats the difference between Integer.parseInt and Integer.valueOf? – HighMrSai Mar 29 '13 at 14:09
  • parseint return type is primitive type. valueof returns the Integer wrapper type.[use this link for better understanding](http://stackoverflow.com/questions/508665/difference-between-parseint-and-valueof-in-java) – Yugesh Mar 29 '13 at 18:04