Matt
Matt

Reputation: 3912

NullPointerException on Dialog box - Android

I am trying to implement a dialog box to appear when a button is clicked. I followed an example to get this below code since I have never worked with dialogs before. I am getting a NPE in 2 separate spots in my Results class. I have commented in my code below both spots.

Thank you in advanced for your help.

Java code:

public class Results extends Activity {

    Button detailsBtn;
    final Context context = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);

        detailsBtn = (Button)findViewById(R.id.detailsBtn);
        detailsBtn.setText("Details");

        detailsBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.resultsdetailsdisplay);
                dialog.setTitle("Detailssss");

                TextView title = (TextView)findViewById(R.id.title);
                title.setText("TITLE - TESTING");  //NULL POINTER EXCEPTION

                Button close = (Button)findViewById(R.id.close);

                close.setOnClickListener(new OnClickListener() {  //NULL POINTER EXCEPTION
                    public void onClick(View arg0) {
                        dialog.dismiss();
                    }
                });
            }
        });
    }
}

resultsdetailsdisplay.xml:

<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/scroll" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:layout_centerHorizontal="true"
        android:gravity="center" />
</RelativeLayout>

LogCat output

02-13 18:36:39.900: E/AndroidRuntime(767): java.lang.NullPointerException
02-13 18:36:39.900: E/AndroidRuntime(767):  at matt.lyons.bibletrivia.Results$4.onClick(Results.java:85)
02-13 18:36:39.900: E/AndroidRuntime(767):  at android.view.View.performClick(View.java:4084)
02-13 18:36:39.900: E/AndroidRuntime(767):  at android.view.View$PerformClick.run(View.java:16966)
02-13 18:36:39.900: E/AndroidRuntime(767):  at android.os.Handler.handleCallback(Handler.java:615)
02-13 18:36:39.900: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 18:36:39.900: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:137)
02-13 18:36:39.900: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:4745)
02-13 18:36:39.900: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 18:36:39.900: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
02-13 18:36:39.900: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-13 18:36:39.900: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-13 18:36:39.900: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1461

Answers (3)

PavanBhushan
PavanBhushan

Reputation: 106

TextView tv = (TextView)dialog.findViewById(R.id.title);

Button close = (Button)dialog.findViewById(R.id.close);

Here in the above xml you're not creating the button also.

Upvotes: 1

ggenglish
ggenglish

Reputation: 1678

use the dialog object:

TextView title = (TextView) dialog.findViewById(R.id.title);

and

Button close = (Button) dialog.findViewById(R.id.close);

As specified here: findViewById - Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).

Your dialog is loading after the fact so only the dialog context knows about these views.

Upvotes: 3

ecem
ecem

Reputation: 3614

Both problems are caused by the same method, findViewById(R.id.something). Since you try to reference your views from an outside class (namely OnClickListener) you cannot use findViewById directly. Instead you may use for example,

Results.this.findViewById(R.id.title);

Hope it works!

Upvotes: 0

Related Questions