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 8375181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:06:30+00:00 2026-06-09T15:06:30+00:00

I try to use my own SurfaceView in XML and I am unable to

  • 0

I try to use my own SurfaceView in XML and I am unable to do it. I get NullPointerException.
According internet it should look like this:
Activity:

package editor;

import android.app.Activity;
import android.os.Bundle;

import com.example.balls_menu_v1.R;

public class EditorActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editor);
    EditorView ev = (EditorView) findViewById(R.id.editorView);

}
}

If I comment findViewById I get NullPointerException.
SurfaceView:

package editor;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class EditorView extends SurfaceView {

    public EditorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        SurfaceHolder holder = getHolder();
        Canvas canvas = holder.lockCanvas();
        canvas.drawColor(Color.GREEN);
        holder.unlockCanvasAndPost(canvas);
    }
}

Layout: editor.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" >

    <editor.EditorView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/editorView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
</RelativeLayout>
  • 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-06-09T15:06:31+00:00Added an answer on June 9, 2026 at 3:06 pm

    I found answer: implement SurfaceHolder.Callback, add all 3 constructors of SurfaceView and add getHolder().addCallback(this); to each constructor.
    Code:

    public class EditorView extends SurfaceView implements SurfaceHolder.Callback{
    
        public EditorView(Context context) {
            super(context);
            getHolder().addCallback(this);
            // TODO Auto-generated constructor stub
        }
    
        public EditorView(Context context, AttributeSet attrs) {
            super(context, attrs);
            getHolder().addCallback(this);
            // TODO Auto-generated constructor stub
        }
    
        public EditorView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            getHolder().addCallback(this);
            // TODO Auto-generated constructor stub
        }
    
        public void doDraw() {
            SurfaceHolder holder = getHolder();
            Canvas canvas = holder.lockCanvas();
            canvas.drawColor(Color.GREEN);
            holder.unlockCanvasAndPost(canvas);
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {}
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            doDraw();
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use this: xml2struct when I use this xml: <XMLname attrib1=Some value>
I would like to use my own custom XML format in my Web.config. In
I try to use a variable in my kshell script but can't get a
i try to use TBXML and getting this error TBXML class method +tbXMLWithURL not
I've subclassed QGraphicsItem into my own custom class, Hexagon. When I try to use
When i try use a webservice i get the following exception. My main question
I am thinking about this problem now for very long. I try to use
I've created my own exception but when I try to use it I receive
When I try to use curl or file_get_contents to read something like http://example.com/python/json/ from
I'm starting a new project soon and I'd like to try to use some

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.