0

我正在尝试实现一个dialog在单击按钮时出现的框。由于我以前从未使用过,因此我按照示例获取了以下代码dialogs。我在Results班上的 2 个不同地点获得了 NPE。我已经在我的代码中评论了这两个地方。

提前感谢您的帮助。

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();
                    }
                });
            }
        });
    }
}

结果详细信息显示.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

使用对话框对象:

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

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

如此处指定: findViewById - 从在 onCreate(Bundle) 中处理的 XML 中查找由 id 属性标识的视图。

您的对话框是在事后加载的,因此只有对话框上下文知道这些视图。

于 2013-02-13T23:29:23.250 回答
1

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

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

在上面的 xml 中,您也没有创建button

于 2013-02-14T09:56:04.257 回答
0

这两个问题都是由相同的方法引起的,findViewById(R.id.something). 由于您尝试从外部类(即OnClickListener)引用您的视图,因此您不能findViewById直接使用。相反,您可以使用例如,

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

希望它有效!

于 2013-02-13T23:27:05.137 回答