Reputation: 1292
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!
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);
}
}
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) {
}
}
<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>
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
Upvotes: 0
Views: 1616
Reputation: 570
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
Upvotes: 1