我有以下代码,它由ListView中包含的按钮的onClick事件调用。
public void viewExerciseHistory(View view) {
// History button click handler
LinearLayout parentRow = (LinearLayout)view.getParent();
TextView exerciseNameTextView = (TextView)parentRow.getChildAt(0);
String exerciseName = (String)exerciseNameTextView.getText();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.exercise_history_dialog);
dialog.setTitle(exerciseName);
TextView headerTextView = (TextView)view.findViewById(R.id.exercise_history_dialog_name_textview);
//headerTextView.setText("History");
Button closeDialogButton = (Button)view.findViewById(R.id.exercise_history_dialog_close_button);
//closeDialogButton.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// dialog.dismiss();
// }
//});
dialog.show();
}
ListView行定义如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="@+id/exercise_history_dialog_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="15sp" />
<Button android:id="@+id/exercise_history_dialog_close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:typeface="monospace"
android:textSize="12sp"
android:textColor="@color/button_blue_text"
android:text="@string/button_text_close"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
上面的代码工作正常并显示对话框,但如果取消注释headerTextView.setText("History")
或closeDialog.setOnClickListener
它会崩溃并出现NullPointerException。
有谁能解释我做错了什么?
编辑 LogCat:
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:19.915: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.945: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:20.435: D/AndroidRuntime(26167): Shutting down VM
10-14 21:36:20.435: W/dalvikvm(26167): threadid=1: thread exiting with uncaught exception (group=0x40e27930)
10-14 21:36:20.445: E/AndroidRuntime(26167): FATAL EXCEPTION: main
10-14 21:36:20.445: E/AndroidRuntime(26167): java.lang.IllegalStateException: Could not execute method of the activity
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3599)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View.performClick(View.java:4204)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$PerformClick.run(View.java:17355)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.handleCallback(Handler.java:725)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Looper.loop(Looper.java:137)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 21:36:20.445: E/AndroidRuntime(26167): at dalvik.system.NativeStart.main(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.reflect.InvocationTargetException
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3594)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 11 more
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.NullPointerException
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.example.workoutlog.ManageWorkouts.viewExerciseHistory(ManageWorkouts.java:269)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 14 more
答案 0 :(得分:2)
因为这一行而崩溃了:
TextView headerTextView = (TextView) findViewById(R.id.exercise_history_dialog_name_textview);
实际上是返回null。
findViewById方法正在主活动的内容视图中查找视图。相反,您希望在视图对象上使用findViewById方法。在视图对象上调用findViewById将允许您获取所追踪的字段。
dialog.setContentView(R.layout.exercise_history_dialog);
TextView headerTextView = (TextView) dialog.findViewById(R.id.exercise_history_dialog_name_textview);
headerTextView.setText("History");
答案 1 :(得分:0)
尝试更改这些
TextView headerTextView = (TextView)findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)findViewById(R.id.exercise_history_dialog_close_button);
到
TextView headerTextView = (TextView)dialog.findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)dialog.findViewById(R.id.exercise_history_dialog_close_button);
您必须从
告诉您想要findViewById
的哪个视图