我有三个片段FragA,FragB,FragC。我通过捆绑"折扣"从FragA到FragB。然后从FragB发送两个参数"折扣"和"地址"去FragC。直到这里一切正常。
然后我必须从FragC回到FragB。我用了一个按钮' backButton'以此目的。但是当按钮点击时,我的应用程序因NPE而崩溃。我已经从FragC向FragB发送了参数,但是在FragB中没有任何参数。
附上我在下面使用的代码..
Fragement A
class FragA extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
{
context = getActivity();
View v = inflater.inflate(R.layout.layout_fragfirst, container, false);
nextButton = (Button) v.findViewById(R.id.buttonnext);
nextButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
fr = new FragB();
Bundle bundle=new Bundle();
bundle.putString("discount", discount);
fr.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
Fragement B
class FragB extends Fragment
{
Context context;
String discount = "";
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
{
context = getActivity();
View v = inflater.inflate(R.layout.layout_fragsec, container, false);
String addressid = "something";
nextButton = (Button) v.findViewById(R.id.buttonnext);
Bundle b = getArguments();
discount = b.getString("discount");
nextButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
fr = new FragC();
Bundle bundle=new Bundle();
bundle.putString("discount", discount);
bundle.putString("address", addressid);
fr.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
Fragement C
class FragC extends Fragment
{
Context context;
String discount = "",addressid="";
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
{
context = getActivity();
View v = inflater.inflate(R.layout.layout_fragthird, container, false);
backButton = (Button) v.findViewById(R.id.buttonnext);
Bundle b = getArguments();
discount = b.getString("discount");
//backButton to go to Fragment B(FragB)
backButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
fr = new FragB();
Bundle bundle=new Bundle();
bundle.putString("discount", discount);
fr.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
LogCat
java.lang.NullPointerException at com.abc.fragments.FragA.onCreateView(FragA.java:114) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) at android.app.BackStackRecord.run(BackStackRecord.java:635) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) 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:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)
请建议我一个解决方案......
答案 0 :(得分:0)
问题可能是NullPointerException .... 当您在onCreateView中提取参数时,但是当您返回时,getArguments必须为您提供null,并且没有参数从FragC传递到FragB ...更好的是您在bundle周围进行null检查
Bundle b = getArguments();
if( b != null )
discount = b.getString("discount");
当您按下后退按钮时,您正在恢复您的过去片段......但在您的代码中,您将进入Frag A> Frag B> Frag C> Frag B ....您正在以错误的方式实现它...只是不要将Frag B添加到片段堆栈