aarelovich
aarelovich

Reputation: 5576

Error when creating a custom view

I'm very new to android. I've been reading a whole lot of tutorials. But I want to get a simple piece of code to work. I want to do a card game. So I thought I'd start with creating a class that draws a rounded rectangle of a specific dimiension (I don't want to sell or share the game so portability is not an issue).

This is the class I wrote:

public class Card extends View{

RectF borders;  
Paint paint;
float rad;

public Card(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    borders.set((float)0.0, (float)0.0, (float)2.5, (float)3.5);
    rad = 1;
    paint.setARGB(0, 117, 217, 222);        
}

protected void onDraw(Canvas canvas){       
    canvas.drawRoundRect(borders, rad, rad, paint);
}

}

I created a Linear layout in xml and then this is code for the main activity:

public class MainActivity extends Activity {

LinearLayout HandLayout;
Card card;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    HandLayout = (LinearLayout)findViewById(R.id.hand_gallery);
    HandLayout.addView(card);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

When I execute that I get an NullPointer Exception. My guess is because I should add a line like card = new Card(context) but I don't know where to get the context...

Thanks for any help!!!

EDIT:

This is the error code

12-14 14:23:36.900: E/AndroidRuntime(22580): FATAL EXCEPTION: main
12-14 14:23:36.900: E/AndroidRuntime(22580): java.lang.RuntimeException: Unable to start activity ComponentInfo{legen.dary/legen.dary.MainActivity}: java.lang.NullPointerException
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.os.Looper.loop(Looper.java:137)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at java.lang.reflect.Method.invokeNative(Native Method)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at java.lang.reflect.Method.invoke(Method.java:511)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at dalvik.system.NativeStart.main(Native Method)
12-14 14:23:36.900: E/AndroidRuntime(22580): Caused by: java.lang.NullPointerException
12-14 14:23:36.900: E/AndroidRuntime(22580):    at legen.dary.Card.<init>(Card.java:18)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at legen.dary.MainActivity.onCreate(MainActivity.java:28)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.Activity.performCreate(Activity.java:5008)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-14 14:23:36.900: E/AndroidRuntime(22580):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-14 14:23:36.900: E/AndroidRuntime(22580):    ... 11 more

I have tried the following changes to code:

public class MainActivity extends Activity {

LinearLayout HandLayout;
Card card;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    HandLayout = (LinearLayout)findViewById(R.id.hand_gallery);
    card = new Card(this);
    HandLayout.addView(card);

}
}

Upvotes: 0

Views: 75

Answers (3)

laalto
laalto

Reputation: 152917

RectF borders;  
Paint paint;
float rad;

public Card(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    borders.set((float)0.0, (float)0.0, (float)2.5, (float)3.5);
    rad = 1;
    paint.setARGB(0, 117, 217, 222);        
}

Your borders and paint are not initialized here. That's what causes the NPE.

To get something displayed, also consider the following:

  • Your alpha value is 0 i.e. completely transparent. Consider using 255 instead.

  • The borders coordinates are close to 0 so whatever gets drawn is quite small. Consider increasing the 2.5 and 3.5 to something bigger.

Upvotes: 1

amit singh
amit singh

Reputation: 1422

write your onCreate() like this-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    HandLayout = (LinearLayout)findViewById(R.id.hand_gallery);
    HandLayout.addView(new Card(this));

}

Upvotes: 1

Y2i
Y2i

Reputation: 3868

use new Card(this) because Activity extends Context

Upvotes: 0

Related Questions