我有ActionBarDialog(DialogFragment),CarAdapter(RecycleView)和SportFragment。我在SportFragment中创建方法(infoMethod)来显示DialogFragment并且工作正常。但是,如果我想调用CarAdapter中的infoMethod不起作用(java.lang.NullPointerException)
SportFragment中的infoMethod
public void infoMethod(){
Bundle args = new Bundle();
args.putString("title", "Dialog with Action Bar");
ActionBarDialog actionbarDialog = new ActionBarDialog();
actionbarDialog.setArguments(args);
actionbarDialog.show(getActivity().getSupportFragmentManager(),
"action_bar_frag");
}
我试图从CarAdapter调用infoMethod
public MyViewHolder(View view) {
super(view);
image= (ImageView) view.findViewById(R.id.imageViewlist);
image.setOnClickListener(this);
}
@Override
public void onClick(View v) {
SportFragment infoFrag = new SportFragment();
if (v.getId() == image.getId()){
v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.imagelist));
infoFrag.infoMethod(); //call infoMethod not work
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
logcat的
09-29 13:30:09.292 18661-18661/com.paijostudio.hitungmasa E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.paijostudio.hitungmasa.fragment.SportFragment.infoMethod(SportFragment.java:210)
at com.paijostudio.hitungmasa.adapter.CarAdapter$MyViewHolder.onClick(CarAdapter.java:73)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
显示DialogFragment的任何解决方案?谢谢
答案 0 :(得分:1)
由于您尝试在应用的两个不同区域中创建对话框,我建议将其移出到自己的类
public class DialogHelper {
public static void infoMethod(FragmentManager fragmentManager) {
Bundle args = new Bundle();
args.putString("title", "Dialog with Action Bar");
ActionBarDialog actionbarDialog = new ActionBarDialog();
actionbarDialog.setArguments(args);
actionbarDialog.show(fragmentManager, "action_bar_frag");
}
}
然后在SportFragment
中使用DialogHelper.infoMethod(getActivity().getSupportFragmentManager());
至于您的CarAdapter
,您需要接受FragmentManager
。
recyclerView.setAdapter(new CarAdapter(getActivity().getSupportFragmentManager()))
...
public class CarAdapter extends RecyclerView.Adapter<MyViewHolder> {
private FragmentManager fragmentManager;
public CarAdapter(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
}
public class MyViewHolder {
...
@Override
public void onClick(View v) {
if (v.getId() == image.getId()){
v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.imagelist));
DialogHelper.infoMethod(fragmentManager);
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
}
答案 1 :(得分:0)
似乎
pieces_remaining_for
确实没有用,因为@SunnySydeUp可能是对的。也许尝试在前面添加行号
显示java.lang.NullPointerException 在com.paijostudio.hitungmasa.fragment.SportFragment.infoMethod(SportFragment.java:210)
非常具体。你试图对一些空的东西做点什么。从这里发生的事情看,很可能就是SportFragment。
我的建议是为了调试而分解。所以我会移动方法
infoFrag.infoMethod(); //call infoMethod not work
在SportFragment之外。并将其移至课程
public void infoMethod(){
意味着SportFragment可以为null。然后检查一下是否有效。如果是这样,那就是我所期望的,你必须解决创建SportFragment的问题。一旦修复了,你就可以退回方法了。请查看在Android文档中获取或创建片段。
https://developer.android.com/guide/components/fragments.html
更新: 作为添加和接收更多信息。由于SportFragment创建不正确,因此Fragment没有附加任何活动。
请查看以下答案,以便正确地与您的片段进行通信 How to create interface between Fragment and adapter?