-1

我有以下代码,由 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
4

2 回答 2

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");
于 2013-10-14T20:58:38.343 回答
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从哪个视图

于 2013-10-14T20:52:52.723 回答