开发者

Update SurfaceView by passing though some variables to this Class

开发者 https://www.devze.com 2023-03-22 06:07 出处:网络
I\'m still quite new to Android and Java programming and even newer to draw images dynamically. What I would like to do is to update a line from within my activity Main. I would like to keep the draw

I'm still quite new to Android and Java programming and even newer to draw images dynamically.

What I would like to do is to update a line from within my activity Main. I would like to keep the drawing separate from Main therefore I added a new file and class called Panel.

When I run my code I get a FC with a java.lang.NullPointerException. When I remove Draw(); from the onCreate() I don't get the FC.

So basically Form within Main.java I would like to calculate some values which I want pass through to Panel to use to draw a figure.

This is a simplified version of my code, but I think I do something fundamentally wrong because this is the first time I use more than 1 java file.

Thanks a lot for your help!

Main.java (simplified)

package com.tricky_design.app;

import com.tricky_design.app.*;

public class Main extends Activity {

    private Panel Drawing;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        Drawing = (Panel) findViewById (R.id.Drawing);
        Draw();

    }

    private void Draw() {
        Drawing.redraw( 10, 20, 30, 40);
    }
}

Panel.java

package com.tricky_design.app;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Panel extends SurfaceView implements SurfaceHolder.Callback{

    private int LineLeft    = 0;
    private int LineRight   = 0;
    private int LineTop     = 0;
    private int LineBottom  = 0;

    Paint paint = new Paint();

    public Panel(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        getHolder().addCallback(this);
    }

    @Override
    public void onDraw(Canvas canvas) {
        DrawLines(canvas);
    }

    public void DrawLines(Canvas canvas) {
        canvas.drawLine(Left, Right, Bottom, Top, paint);
    }

    public void redraw(int Left, int Right, int Top, int Bottom) {
        LineLeft   = Left;
        LineRight  = Right;
        LineTop    = Top;
        LineBottom = Bottom;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas c = getHolder().lockCanvas();
        draw(c);
        getHolder().unlockCanvasAndPost(c);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

layout.xml (simplified)

 <com.tricky_design.app.Panel
     android:id="@+id/Drawing"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#FC123456">
 </com.tricky_design.app.Panel>

Edit 21-07-2011 18:52 - Output logcat

07-21 18:48:34.501: ERROR/AndroidRuntime(12385): FATAL EXCEPTION: main
07-21 18:48:34.501: ERROR/AndroidRuntime(12385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tricky_design.app/com.tricky_design.app.main}: java.lang.NullPointerException
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.os.Looper.loop(Looper.java:143)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.ActivityThread.main(ActivityThread.java:4196)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at java.lang.reflect.Method.invoke(Method.java:507)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at dalvik.system.NativeStart.开发者_高级运维main(Native Method)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385): Caused by: java.lang.NullPointerException
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at com.tricky_design.app.main.Draw(main.java:376)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at com.tricky_design.app.main.init(main.java:311)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at com.tricky_design.app.main.onCreate(main.java:245)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
07-21 18:48:34.501: ERROR/AndroidRuntime(12385):     ... 11 more


There are several reasons why your code posted above will not work. TO get an example of how a surfaceview should work look at the lunar lander code in the googleAPIs which should come with the Android SDK download. http://developer.android.com/resources/samples/LunarLander/index.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号