0

dialogボタンがクリックされたときに表示されるボックスを実装しようとしています。これまで作業したことがないので、例に従って以下のコードを取得しましたdialogs。クラスの2つの別々の場所でNPEを取得していResultsます。私は両方のスポットの下のコードにコメントしました。

よろしくお願いします。

Javaコード:

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出力

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)
4

3 に答える 3

3

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.

于 2013-02-13T23:29:23.250 に答える
1

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.

于 2013-02-14T09:56:04.257 に答える
0

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!

于 2013-02-13T23:27:05.137 に答える