创建自定义视图时出错

时间:2013-12-14 17:20:48

标签: android view

我对android很新。我一直在阅读很多教程。但我希望得到一段简单的代码。我想做一个纸牌游戏。所以我想我开始创建一个绘制圆形矩形特定目标的类(我不想出售或分享游戏,因此可移植性不是问题)。

这是我写的课程:

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

}

我在xml中创建了一个线性布局,然后这是主要活动的代码:

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

}

当我执行时,我得到一个NullPointer异常。我的猜测是因为我应该添加一行像card = new Card(context)但我不知道从哪里获取上下文......

感谢您的帮助!!!

编辑:

这是错误代码

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

我已尝试对代码进行以下更改:

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

}
}

3 个答案:

答案 0 :(得分:1)

像这样写你的onCreate() -

@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));

}

答案 1 :(得分:1)

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

您的borderspaint未在此初始化。这就是造成NPE的原因。

要显示某些内容,请考虑以下事项:

  • 您的Alpha值为0,即完全透明。请考虑使用255代替。

  • borders坐标接近于0,因此绘制的内容非常小。考虑将2.53.5增加到更大的范围。

答案 2 :(得分:0)

使用new Card(this)因为Activity扩展了上下文