我正在尝试实现一个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)