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

  • Home
  • SEARCH
  • 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 9148617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:19:29+00:00 2026-06-17T11:19:29+00:00

Im having a problem with a nullpointer on my findViewById() its a very simple

  • 0

Im having a problem with a nullpointer on my findViewById()
its a very simple app it loads into the AnalogClock activity first with just a analog clock and button to go to the DigitalClock activity first activity loads up fine but when the button is clicked the viewById() in the onCreate of the DigitalClock activity returns null and then crashes.’

AnalogActivity.java

package com.example.dualclocks2;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AnalogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analog);

    Button go2DigitalButton = (Button) findViewById(R.id.btnGo2Digital);
    go2DigitalButton.setOnClickListener(go2DigitalButtonListener);

}

public OnClickListener go2DigitalButtonListener = new OnClickListener() 
   {

    @Override
    public void onClick(View v) {


         Intent intent = new Intent(AnalogActivity.this, DigitalActivity.class);            

         startActivity(intent); // execute the Intent
    }

   };

@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_analog, menu);
    return true;
}
}

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

<AnalogClock
    android:id="@+id/analogClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="150dp" />

<Button
    android:id="@+id/btnGo2Digital"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/analogClock1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="62dp"
    android:text="@string/go2DigitalClock" />
</RelativeLayout>

DigitalActivity.java

    package com.example.dualclocks2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DigitalActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_digital);

    Button go2AnalogButton = (Button) findViewById(R.id.btnGo2Digital);//null here
    go2AnalogButton.setOnClickListener(go2AnalogButtonListener);
}

public OnClickListener go2AnalogButtonListener = new OnClickListener() 
   {

    @Override
    public void onClick(View v) {


         Intent intent = new Intent(DigitalActivity.this, AnalogActivity.class);            

         startActivity(intent); // execute the Intent
    }

   };

@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_digital, menu);
    return true;
}

}

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

<Button
    android:id="@+id/btnGo2Analog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="69dp"
    android:text="@string/go2Analog" />

<DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="DigitalClock"
    android:textSize="@dimen/digitalClockSize" />

</RelativeLayout>

LogCat

01-18 17:05:35.835: E/AndroidRuntime(15800): FATAL EXCEPTION: main
01-18 17:05:35.835: E/AndroidRuntime(15800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dualclocks2/com.example.dualclocks2.DigitalActivity}: java.lang.NullPointerException
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.os.Looper.loop(Looper.java:130)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.main(ActivityThread.java:3688)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at java.lang.reflect.Method.invoke(Method.java:507)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at dalvik.system.NativeStart.main(Native Method)
01-18 17:05:35.835: E/AndroidRuntime(15800): Caused by: java.lang.NullPointerException
01-18 17:05:35.835: E/AndroidRuntime(15800):    at com.example.dualclocks2.DigitalActivity.onCreate(DigitalActivity.java:19)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-18 17:05:35.835: E/AndroidRuntime(15800):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-18 17:05:35.835: E/AndroidRuntime(15800):    ... 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-06-17T11:19:30+00:00Added an answer on June 17, 2026 at 11:19 am
    R.layout.activity_digital
    

    does not contain a Button whose id is btnGo2Digital.

    It seems you wanted R.id.btnGo2Analog instead and meant to write

    Button go2AnalogButton = (Button) findViewById(R.id.btnGo2Analog);//changed id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple error I'm sure, just having a mental block for some reason! I
I am trying to write my first Android app, and I'm having trouble. I
i having problem in converting this C# code into VB.net. The loadLecturer seem to
I am having problem in django. I have created a form in my app
i having problem understanding what is wrong with my inheritance. I just can not
Im having problem in importing hotmail contacts from hotmail in php it simple give
Im having problem with cordova xcode app. I already have my html page setup.
Today is my first day learning java :) I'm having problems running a very
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
Im having problem in my UIscrollView ,this is what I have done: Whenever a

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.