getArguments()在第一次为Frag1调用OnCreateView()时返回正确的JSON字符串,但是当我滑动到Frag2并且再次调用Frag1的OnCreateView()时则不返回。相反,“JSON”参数为null。
的活动:
SparkleViewPagerLayout sparkleViewPagerLayout = (SparkleViewPagerLayout)
findViewById(R.id.view_pager_layout);
FragmentManager fm = getSupportFragmentManager();
sparkleViewPagerLayout.getViewPager().setAdapter(new FragmentPagerAdapter(fm) {
@Override
public int getCount() {
return 4;
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return Frag0.newInstance(json0);
case 1:
return Frag1.newInstance(json1); // <--THE FAILING FRAGMENT
case 2:
return Frag2.newInstance(json2);
case 3:
return Frag3.newInstance(json3);
default:
return Frag0.newInstance(json0);
}
}
});
片段:
public class Frag1 extends Fragment {
private ArrayList<Thing> things;
private Gson gson = new Gson();
public static Frag1 newInstance(String json) {
Bundle args = new Bundle();
args.putString("JSON", json);
args.putInt("background", R.drawable.img_thing_bg_blurred);
Frag1 fragment = new Frag1();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.thing_detailed, parent, false);
int background = getArguments().getInt("background");
Util.setBackground(v, background, getContext());
String json = getArguments().getString("JSON");
things = gson.fromJson(json, new TypeToken<ArrayList<Thing>>() {
}.getType());
setUpThingOne(v);
FragmentManager fm = getChildFragmentManager();
Fragment listFragment = fm.findFragmentById(R.id.thingFragmentListContainer);
// We removed the thing in position one, need to update the JSON to reflect this change.
String jsonNew = gson.toJson(things);
if (listFragment == null) {
Bundle bundle = new Bundle();
bundle.putString("THINGLIST", jsonNew);
listFragment = new ThingListFragment();
listFragment.setArguments(bundle);
fm.beginTransaction()
.add(R.id.ThingFragmentListContainer, listFragment)
.commit();
}
return v;
}
private void setUpThingOne(View v) {
TextView name = (TextView) v.findViewById(R.id.thingName);
TextView type = (TextView) v.findViewById(R.id.thingField);
ImageView dollarImg = (ImageView) v.findViewById(R.id.thingPrice);
ImageView bgImg = (ImageView) v.findViewById(R.id.thingBg);
for (Thing thing : things) {
if (thing.getPosition() == 1) {
name.setText(thing.getName());
type.setText(thing.getField());
Util.setDollarImage(getContext(), dollarImg, thingSpot.getPrice());
RequestUtil.getImage2(getContext(), thing.getImage_url(), bgImg);
//Remove the #1 thing so that we can make a fragment list from the others.
//things.remove(thing);
break;
}
}
}
}
崩溃日志:
01-28 13:43:08.117 31378-31378/com.test.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.test, PID: 31378
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
at com.test.test.fragments.Frag1.setUpThingOne(Frag1.java:78)
at com.test.test.fragments.Frag1.onCreateView(Frag1.java:48)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
视图寻呼机将您的片段存储在内存中。可能是方法newInstance
仅在第一次创建片段时运行。当它在屏幕上不可见时,可以调用其onDestroyView()
,然后再次显示它并调用onCreateView()
回调(newInstance
将永远不会被触发)。这就解释了为什么onCreateView()
上的参数为空。您在所有片段回调中调用put log
以查看片段的状态。检查Android以获取有关片段生命周期的更多详细信息。
答案 1 :(得分:0)
好吧,结果我做了一些复制粘贴时并不是很小心。我认为当我刷到Frag2时onCreateView()被调用为Frag1并且我意识到我在Frag2中创建了一个新的Frag1而不是Frag2时,我觉得很奇怪......很多小时浪费在本来应该没有发生的事情上!