我想創建自定義視圖,在屏幕上畫圓。但不斷得到一些錯誤。如果我不在CustomView構造函數中進行任何初始化,它可以嗎?或者應該在那裏初始化哪些數據?創建自定義視圖對象
這是我的代碼巫不起作用:
package com.example.anime;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
public Paint paint = new Paint();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout frame = (RelativeLayout) findViewById(R.layout.activity_main);
final CustomView mv = new CustomView(getApplicationContext());
frame.addView(mv);
}
public class CustomView extends View{
public CustomView(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.RED);
canvas.drawCircle(50, 50, 50, paint);
}
}
}
XML佈局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@color/green"
android:id="@+id/frame" >
</RelativeLayout>
登錄:
03-06 16:15:18.691: D/AndroidRuntime(1460): Shutting down VM
03-06 16:15:18.691: W/dalvikvm(1460): threadid=1: thread exiting with uncaught exception (group=0xa4cfeb20)
03-06 16:15:18.695: E/AndroidRuntime(1460): FATAL EXCEPTION: main
03-06 16:15:18.695: E/AndroidRuntime(1460): Process: com.example.anime, PID: 1460
03-06 16:15:18.695: E/AndroidRuntime(1460): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anime/com.example.anime.MainActivity}: java.lang.NullPointerException
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.os.Looper.loop(Looper.java:136)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-06 16:15:18.695: E/AndroidRuntime(1460): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 16:15:18.695: E/AndroidRuntime(1460): at java.lang.reflect.Method.invoke(Method.java:515)
03-06 16:15:18.695: E/AndroidRuntime(1460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-06 16:15:18.695: E/AndroidRuntime(1460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-06 16:15:18.695: E/AndroidRuntime(1460): at dalvik.system.NativeStart.main(Native Method)
03-06 16:15:18.695: E/AndroidRuntime(1460): Caused by: java.lang.NullPointerException
03-06 16:15:18.695: E/AndroidRuntime(1460): at com.example.anime.MainActivity.onCreate(MainActivity.java:31)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.Activity.performCreate(Activity.java:5231)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-06 16:15:18.695: E/AndroidRuntime(1460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-06 16:15:18.695: E/AndroidRuntime(1460): ... 11 more
感謝您的幫助!
TNX的大小!你能告訴我關於invalidate()和postInvalidate()方法嗎?他們用於什麼? –
@TheDubleM刷新視圖。 onDraw get的調用 – Raghunandan