JosephK
JosephK

Reputation: 101

NullPointer error from findViewById

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

Upvotes: 1

Views: 259

Answers (1)

A--C
A--C

Reputation: 36449

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

Upvotes: 5

Related Questions