我有使用arrayAdapter填充的ListView(在Fragment内部)。我在listView的每个视图中都有应用按钮。我做了一个应用按钮的监听器,我想点击按钮后开始新的片段。我在getFragmentManager()得到了nullPointerException。我尝试了this,this also但无法解决我的问题。
这是我的ArrayAdaptor代码:
public class FullTimeAdapter extends ArrayAdapter<Datalist> {
private Context mContext;
private FullTimeJobs mFragment;
private List<Datalist> datalists = new ArrayList<>();
public FullTimeAdapter(@NonNull Context context, ArrayList<Datalist> list) {
super(context, 0, list);
mContext = context;
datalists = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
View listView = convertView;
if (listView == null) {
listView = LayoutInflater.from(mContext).inflate(R.layout.full_time_datalist, parent, false);
}
Button btnApply = listView.findViewById(R.id.part_btn_apply);
btnApply.setText("Apply");
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment;
fragment = new Apply();
FragmentManager fm = fragment.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction().replace(R.id.container, fragment);
ft.commit();
}
});
return listView;
}
}
logcat的:
05-16 17:00:59.566 10150-10150/com.example.innobles.internapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.innobles.internapp, PID: 10150
java.lang.NullPointerException
at com.example.innobles.internapp.Adapter.FullTimeAdapter$2.onClick(FullTimeAdapter.java:90)
at android.view.View.performClick(View.java:4448)
at android.view.View$PerformClick.run(View.java:18461)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
首先创建一个接口
interface CustomOnClick {
void onCustomOnClick();
}
在您应用适配器的Fragment中实现此接口,并在适配器构造函数中传递您的活动实例在您的适配器中创建接口对象,如
CustomOnClick listener;
并在适配器构造函数中初始化它
public FullTimeAdapter(@NonNull Context context, ArrayList<Datalist> list, CustomOnClick listener) {
super(context, 0, list);
this.listener=listener;
mContext = context;
datalists = list;
}
在适配器点击方法中的只需编写以下行并在Fragments的onCustomClick()方法中移动剩余代码
listener.onCustomClick();
在你的framents'onCustomClick方法中使用getActivity()。getFragmentManger()
答案 1 :(得分:0)
尝试FragmentManager fm = getActivity().getFragmentManager();
答案 2 :(得分:0)
最后它有效,但不知道如何......
这里我做了什么:
((AppCompatActivity) mContext).getSupportFragmentManager().beginTransaction().replace(R.id.container, new Apply()).commit();
而不是
getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();