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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:48:44+00:00 2026-06-14T11:48:44+00:00

My code is ok but unfortunately my code gives me null pointer exception. I

  • 0

My code is ok but unfortunately my code gives me null pointer exception. I know that by writing the setcontentview before findviewbyid it wont happen but then by doing so my surface does not update.I tried lot searching about this but i am not able to get proper answer for this please help me.My code for mainctivity:-

package com.example.snakes;

public class MainActivity extends Activity implements View.OnClickListener{

MySurfaceClass sc;
Button up,down,left,right;
//View v1,v2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    sc = new MySurfaceClass(this);
    //setContentView(R.layout.activity_main);

    up = (Button) findViewById(R.id.button1);
    down = (Button) findViewById(R.id.button2);
    left = (Button) findViewById(R.id.button3);
    right = (Button) findViewById(R.id.button4);

    up.setOnClickListener(this);
    down.setOnClickListener(this);
    left.setOnClickListener(this);
    right.setOnClickListener(this);
    setContentView(R.layout.activity_main);

    // Toast.makeText(this, "done", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View v) {
    switch(v.getId()){
    case R.id.button1:
        sc.posY = sc.posY - 50 ;
        Toast.makeText(this, "up", Toast.LENGTH_SHORT).show();
    //   sc = new MySurfaceClass(this);
      //   setContentView(R.layout.activity_main);
        break;

    case R.id.button2:
        sc.posY = sc.posY + 50 ;
        Toast.makeText(this, "down", Toast.LENGTH_SHORT).show();
        // sc = new MySurfaceClass(this);
         //setContentView(R.layout.activity_main);
        break;

    case R.id.button3:
        sc.posX = sc.posX - 50 ;
        Toast.makeText(this, "left", Toast.LENGTH_SHORT).show();
        //sc = new MySurfaceClass(this);
        //setContentView(R.layout.activity_main);
        break;

    case R.id.button4:
        sc.posX = sc.posX + 10 ;
        Toast.makeText(this, "right", Toast.LENGTH_SHORT).show();
        //setContentView(R.layout.activity_main);
        break;
    }
}
}

while the code for surface activity:-

package com.example.snakes;


public class MySurfaceClass extends SurfaceView implements Runnable {

private SurfaceHolder sh;
private Thread t = null;
private boolean isRunning;
private Bitmap b;
protected int posX, posY;

public MySurfaceClass(Context context) {
    super(context);
    init();
}

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

public MySurfaceClass(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

void init() {
    isRunning = true;
    b = BitmapFactory.decodeResource(getResources(), R.drawable.face);
    posX = 0;
    posY = 0;
    sh = this.getHolder();
    t = new Thread(this);
    t.start();
};

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(100, 100, 100, 100, null);
}

public void run() {
    while (isRunning) {
        if (!sh.getSurface().isValid())
            continue;

        Canvas c = sh.lockCanvas();
        c.drawRGB(5, 150, 10);
        c.drawBitmap(b, posX, posY, null);
        sh.unlockCanvasAndPost(c);
    }
}
  }

and the xml :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



<LinearLayout
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/grey"
    android:weightSum="100" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="up" 
        android:layout_weight="25"/>

    <Button
        android:id="@+id/button2"
        android:layout_weight="25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="down" />

    <Button
        android:id="@+id/button3"
        android:layout_weight="25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left" />

    <Button
        android:id="@+id/button4"
        android:layout_weight="25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.snakes.MySurfaceClass
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/surfaceView"
        />
</FrameLayout>

  • 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-14T11:48:45+00:00Added an answer on June 14, 2026 at 11:48 am

    setContentView method has to be called before getting any references to widgets with findViewById otherwise you will get NullPointerExceptions when setting the click listeners to the buttons.

    I would make the buttons work first, then worry about the surface view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some code which works fine in IE but unfortunately not in Google
I tried below code but as I run it gives a black screen image
I have the following code but the lon/lat seems to be returning null; package
I have write this code but it does work only if I am already
I have the following code but it doesnt seem to work: <td align=center style=padding-bottom:52.5px>
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
Hey I have this code but it doesn't work because it is expecting a
I'm using following code but cannot return data from MySQL. This is the output:
I have the following code but I am unsure how I would adjust it
I am having following code but unable to understand as to why no match

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.