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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:55:10+00:00 2026-06-18T09:55:10+00:00

New to programming android apps and keep getting a null pointer exception and im

  • 0

New to programming android apps and keep getting a null pointer exception and im not sure why. Ive ressearched this a bit and it seems that most people get this because of the setContentView but it seems that from what everyone else is saying my setContentView is in the right space etc…

int randInt;
int[] randColor = {Color.RED,Color.BLUE,Color.GREEN,Color.YELLOW,Color.BLACK,
                Color.CYAN,Color.MAGENTA,Color.LTGRAY,Color.DKGRAY,Color.WHITE};
TextView textView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this);
    View layout = findViewById(R.layout.activity_main);
    layout.setOnTouchListener(this);
    textView1 = (TextView) findViewById(R.id.textView1);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
    public void onClick(View v) {
        setBackGroundColor(randInt);
    }

@Override
public boolean onTouch(View v, MotionEvent e){
    float xPos = e.getX();
    float yPos = e.getY();
    changeText(xPos,yPos);
    return true;
}

public void setBackGroundColor(int randInt){
    getWindow().setBackgroundDrawable(new ColorDrawable(randColor[getRandomInt()]));
}

public int getRandomInt(){
        Random randomInt = new Random();
        randInt = randomInt.nextInt(10);
        return randInt;
}

public void changeText(float xPos, float yPos){
    textView1.setText("You are at " + xPos + " , " + yPos);
}

}

<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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="66dp"
    android:text="@string/button" />

  • 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-18T09:55:11+00:00Added an answer on June 18, 2026 at 9:55 am

    Assign an id to your layout:

    <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"
    tools:context=".MainActivity" 
    android:id="@+id/my_layout">
    

    Then change your code as so:

    View layout = findViewById(R.id.my_layout);
    

    findViewByID(), like the method name says, needs an ID. Passing off a layout does pass an int, so it compiles fine, but it’s unlikely you’ll match against an id, and the method returns null.

    If anything else also returns null, make sure your layout (and its children views) are contained in the xml file called activity_main.xml and that you have cleaned your project.

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

Sidebar

Related Questions

New to android programming. Getting Following error. 04-07 14:49:05.452: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main java.lang.OutOfMemoryError
I am new to android programming and i am trying to develop this app
I'm new to programming for android and am a bit annoyed by an unexpected
I am new to Android programming and so kingly pardon me if this question
I'm new here and new in programming Android. I found this example (below is
I am new to Android but not to programming. I have taken C++ in
So, I'm both new to Java and to creating android apps, but not new
I am new to Android programming and so kingly pardon me if this question
i am new to programming and hence android programming. I'm making an android app
I'm still new to programming/Java/Android so I'm trying to understand everything I do and

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.