【问题标题】:I want my button to call a method in a fragment我希望我的按钮调用片段中的方法
【发布时间】:2015-06-15 08:14:15
【问题描述】:

所以我有 (WorkoutList Activity) -> (WorkoutList Fragment) -> (ExerciseList Fragment) -> (addExercise Fragment)

AddExercise 中有一个按钮,我希望它调用ExerciseList 中的一个方法

问题在于,在当前设置下,我正在尝试调用虚拟方法

void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

AddExercise 按钮​​的 XML:

<Button
    android:id="@+id/add_exercise_button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/add_workout_message_done"
    android:layout_weight="0"
    android:onClick="addExerciseDone"
    />

在 OnCreateView 方法中的 ExerciseList 的代码 sn-p:

     View rootView = inflater.inflate(R.layout.fragment_exercise_list, container, false);
    Button AddExerciseDoneButton = (Button) getActivity().findViewById(R.id.add_exercise_button);
    AddExerciseDoneButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addExerciseDone(v);
        }
    });

注意: 这是对chinmay的建议的回应** 我将viewroot设置为ExerciseList的XML文件,这样我就可以让我的适配器显示我的列表视图,所以我不确定如何返回我创建的视图,以便我可以引用按钮。比如我做了

final View tmpView = inflater.inflate(R.layout.fragment_add_exercise, container, false);

    Button AddExerciseDoneButton = (Button) tmpView.findViewById(R.id.add_exercise_button);

【问题讨论】:

  • setContentView 不适用于Fragment。而不是使用onCreateView() 方法。你的问题也不清楚。

标签: android button fragment


【解决方案1】:

您收到 NullPointerException 是因为您对 AddExerciseDoneButton 的引用无效。 您尚未指定任何具有按钮 ID - R.id.add_exercise_button 的布局,当您尝试使用该按钮时,它会给出 NullPointer

相反,您的 onCreateView 应该看起来像

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

使您的视图引用 onCreateView 返回的视图,它将获得按钮 ID

供参考:http://developer.android.com/guide/components/fragments.html

你正在这样做

View rootView = inflater.inflate(R.layout.fragment_exercise_list, container, false);
    Button AddExerciseDoneButton = (Button) getActivity().findViewById(R.id.add_exercise_button);

而不是这个

     View rootView = inflater.inflate(R.layout.fragment_exercise_list, container, false);
        Button AddExerciseDoneButton = (Button) 
rootView.findViewById(R.id.add_exercise_button);

【讨论】:

  • 如果我已经返回用于设置适配器的 rootView 怎么办?
  • 按钮位于另一个名为 fragment_add_exercise 的片段中,我是否必须将此代码放在该类的 onCreateView 中?如果是这样,我遇到了一个问题,我无法从静态上下文中引用非静态上下文
  • 抱歉,AddExerciseDoneButton 与练习列表位于单独的文件中。我无法更改 rootView 的布局,因为我需要更改列表适配器。我现在正在尝试将 AddExerciseDoneButton 的代码移动到 addExercise 类。我上次尝试这种方法时遇到的问题是我无法从静态类中引用非静态类。我想引用练习列表的原因是我可以在列表中添加一些内容。
  • 如果与您尝试使用的当前片段中的布局无关,我无法在此片段中使用该按钮
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-08
  • 2017-04-28
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-05
相关资源
最近更新 更多