Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6171267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:10:59+00:00 2026-05-23T23:10:59+00:00

I’m still quite new to Android and Java programming and even newer to draw

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:11:00+00:00Added an answer on May 23, 2026 at 11:11 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.