创建自定义View对象

时间:2014-03-06 16:24:57

标签: android view ondraw

我想创建自定义视图,在屏幕上绘制圆圈。但不断得到一些错误。如果我不在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

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

更改此

RelativeLayout frame = (RelativeLayout) findViewById(R.layout.activity_main);

RelativeLayout frame = (RelativeLayout) findViewById(R.id.frame);

http://developer.android.com/reference/android/app/Activity.html#findViewById(int)

答案 1 :(得分:1)

更改

RelativeLayout frame = (RelativeLayout) findViewById(R.layout.activity_main);

RelativeLayout frame = (RelativeLayout) findViewById(R.id.frame);

答案 2 :(得分:1)

修复findViewById后,您可能需要添加

mv.setLayoutParams(new ViewGroup.LayoutParams(110,110));

在addView之前选择CustomView的大小