Frage

I want to create custom View that paint circle on screen. But constantly getting some errors. Is it OK if I don't put any initialization in CustomView constructor? Or what data should initialize there?

This is my code witch doesn't work:

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 layout:

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

Log:

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

Thanks for helping!

War es hilfreich?

Lösung

Change

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

with

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

Andere Tipps

Change this

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

to

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

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

After fixing your findViewById you'll probably need to add

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

before the addView to choose the size of your CustomView

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top