从服务器接收响应时出错

时间:2015-03-17 06:08:47

标签: android android-fragments android-activity otto

我对ott+activity+fragment.有疑问 1.我打开活动并在onCreate中注册otto。

Observer.getInstance().register(this);

2。如果我的方法完成,我将向服务器发送请求

@Override
    public void didScanBarcode(String barcode, String symbology) {
      ...
        ItemApiConnector.me().getItem(cleanedBarcode);
    }

3。等等这个方法

@Subscribe(MessageType.ITEM_SUCCESS)
    public void getItemFromServer(Item item) {
        Fragment fragment =  ItemFragment.newInstance(item);
        setFragment(fragment);
}

4。我的片段

public class ItemFragment extends Fragment {
    private Item item;
    private TextView tvName;
    private TextView tvPrice;
    private ImageView itemImage;
    private DisplayImageOptions options;
    AnimateFirstDisplayListener animateFirstDisplayListener = new AnimateFirstDisplayListener();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.item_info, container, false);
        Bundle bundle = getArguments();
        item  = (Item) bundle.getSerializable("item");
        tvName = (TextView) rootView.findViewById(R.id.tvItemName);
        tvPrice = (TextView) rootView.findViewById(R.id.tvPrice);
        itemImage = (ImageView) rootView.findViewById(R.id.itemImage);
        tvName.setText(item.getName());
        tvPrice.setText("$"+item.getPrice());
        options = ILOptions.getOption();
        ImageLoader.getInstance().displayImage(item.getImage(), itemImage, options,         animateFirstDisplayListener);
        return rootView;
    }

    public static ItemFragment newInstance(Item item) {
        ItemFragment f = new ItemFragment();
        Bundle b = new Bundle();
        b.putSerializable("item", item);
        f.setArguments(b);
        return f;
    }
}

第一次一切正常。然后我关闭Activity。再次打开。和一个错误

java.lang.RuntimeException: Could not dispatch event: class com.skip.client.models.Item to handler [EventHandler public void com.skip.client.customer.activities.ScanActivity.getItemFromServer(com.skip.client.models.Item)]: Can not perform this action after onSaveInstanceState
            at com.skip.client.core.otto.Bus.throwRuntimeException(Bus.java:458)
            at com.skip.client.core.otto.Bus.dispatch(Bus.java:388)
            at com.skip.client.core.otto.Bus.dispatchQueuedEvents(Bus.java:369)
            at com.skip.client.core.otto.SkipBus.post(SkipBus.java:52)
            at com.skip.client.core.Observer.send(Observer.java:26)
            at com.skip.client.connector.DefaultCallback.onSuccess(DefaultCallback.java:134)
            at com.skip.client.connector.DefaultCallback.success(DefaultCallback.java:55)
            at com.skip.client.connector.DefaultCallback.success(DefaultCallback.java:23)
            at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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)
     Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1365)
            at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1383)
            at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)
            at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615)
            at com.skip.client.customer.activities.ScanActivity.setFragment(ScanActivity.java:128)
            at com.skip.client.customer.activities.ScanActivity.getItemFromServer(ScanActivity.java:135)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.skip.client.core.otto.EventHandler.handleEvent(EventHandler.java:89)
            at com.skip.client.core.otto.Bus.dispatch(Bus.java:386)
            at com.skip.client.core.otto.Bus.dispatchQueuedEvents(Bus.java:369)
            at com.skip.client.core.otto.SkipBus.post(SkipBus.java:52)
            at com.skip.client.core.Observer.send(Observer.java:26)
            at com.skip.client.connector.DefaultCallback.onSuccess(DefaultCallback.java:134)
            at com.skip.client.connector.DefaultCallback.success(DefaultCallback.java:55)
            at com.skip.client.connector.DefaultCallback.success(DefaultCallback.java:23)
            at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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)

1 个答案:

答案 0 :(得分:3)

我建议您在onResumeonPause注册和取消注册otto。 如果我说得对,你的代码就像这样:

  1. 您在onCreate注册了事件处理程序。
  2. 有些事情可以正常运作。
  3. 应用程序转到后台,一般情况下,如果Activity对象被破坏并收集为垃圾,但它可以保存其参考,因此gc无法收集它。
  4. 再次打开应用程序,创建另一个Activity对象并注册
  5. 某事发生了一个事件,并且otto将它传递给一个旧的活动,该活动本应该被销毁但仍然活着,因为otto保留了它的参考。应用程序因异常而崩溃,因为该活动已处于生命周期的末尾。
  6. 总结一下:最好不要保留对可以随时销毁的对象的引用,因此在这种情况下,当应用程序转到后台时,您应取消注册Activity