Note that there are some explanatory texts on larger screens.

plurals
  1. POError when creating a custom view
    primarykey
    data
    text
    <p>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). </p> <p>This is the class I wrote:</p> <pre><code>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); } } </code></pre> <p>I created a Linear layout in xml and then this is code for the main activity:</p> <pre><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); 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; } } </code></pre> <p>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...</p> <p>Thanks for any help!!!</p> <p>EDIT: </p> <p>This is the error code</p> <pre><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.&lt;init&gt;(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 </code></pre> <p>I have tried the following changes to code:</p> <pre><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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload