0

应用程序/build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

dependencies {
...
implementation 'com.hannesdorfmann.mosby3:mvi:3.1.0'
    implementation 'com.hannesdorfmann.mosby3:mvp:3.1.0'
    implementation 'com.hannesdorfmann.mosby3:viewstate:3.1.0'
implementation 'com.hannesdorfmann.mosby3:mvp-lce:3.1.0'

}

这是我的活动:

import com.hannesdorfmann.mosby3.mvp.MvpActivity;
    public class OfferDetailsPdfActivity extends MvpActivity<OfferDetailsPdfMvp.View, OfferDetailsPdfPresenterImplMvp> implements OnPageChangeListener, OfferDetailsPdfMvp.View {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    }

    }

onCreate()我的活动OfferDetailsPdfActivity的方法 是成功调用。

但我需要将参数传递给活动,所以这里的生产代码:

public class OfferDetailsPdfActivity extends MvpActivity<OfferDetailsPdfMvp.View, OfferDetailsPdfPresenterImplMvp> implements OnPageChangeListener, OfferDetailsPdfMvp.View {
private OfferDetailsPdfPresenterImplMvp presenter;
    private int offerId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        Debug.d(TAG, "onCreate: extras = " + AndroidUtil.bundle2String(extras)
                + "\nsavedInstanceState = " + AndroidUtil.bundle2String(savedInstanceState)
        );
        if (extras != null) {
            offerId = extras.getInt(Offer.ID);
        }
            setContentView(R.layout.offer_details_pdf);
        ButterKnife.bind(this);
    }

    @NonNull
    @Override
    public OfferDetailsPdfPresenterImplMvp createPresenter() {
        Debug.d(TAG, "createPresenter: offerId = " + offerId);
        presenter = new OfferDetailsPdfPresenterImplMvp(this, offerId);
        return presenter;
    }

现在在我的活动中方法onCreate()不是调用,而是方法createPresenter()被成功调用。有文本“createPresenter:offerId = 0”

从 logcat 可以看出,该方法onCreate()没有被调用。在 logcat 中没有文本“ onCreate: extras...

为什么?

这里logcat:

D/com.myproject.android.customer.ui.OfferDetailsPdfActivity(  946): createPresenter: offerId = 0
        Process: com.myproject.android.customer.debug, PID: 30127
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.android.customer.debug/com.myproject.android.customer.ui.OfferDetailsPdfActivity}: java.lang.IllegalArgumentException: Null objects cannot be copied from Realm.
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
           at android.app.ActivityThread.-wrap11(ActivityThread.java)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           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)
        Caused by: java.lang.IllegalArgumentException: Null objects cannot be copied from Realm.
           at io.realm.Realm.checkValidObjectForDetach(Realm.java:1625)
           at io.realm.Realm.copyFromRealm(Realm.java:1318)
           at io.realm.Realm.copyFromRealm(Realm.java:1292)
           at com.myproject.android.customer.service.OfferService.getOffer(OfferService.java:54)
           at com.myproject.android.customer.mvp.presenter.OfferDetailsPdfPresenterImplMvp.loadData(OfferDetailsPdfPresenterImplMvp.java:64)
           at com.myproject.android.customer.mvp.presenter.OfferDetailsPdfPresenterImplMvp.<init>(OfferDetailsPdfPresenterImplMvp.java:59)
           at com.myproject.android.customer.ui.OfferDetailsPdfActivity.createPresenter(OfferDetailsPdfActivity.java:76)
           at com.myproject.android.customer.ui.OfferDetailsPdfActivity.createPresenter(OfferDetailsPdfActivity.java:35)
           at com.hannesdorfmann.mosby3.mvp.delegate.ActivityMvpDelegateImpl.createViewIdAndCreatePresenter(ActivityMvpDelegateImpl.java:90)
           at com.hannesdorfmann.mosby3.mvp.delegate.ActivityMvpDelegateImpl.onCreate(ActivityMvpDelegateImpl.java:142)
           at com.hannesdorfmann.mosby3.mvp.MvpActivity.onCreate(MvpActivity.java:42)
           at com.myproject.android.customer.ui.OfferDetailsPdfActivity.onCreate(OfferDetailsPdfActivity.java:58)
           at android.app.Activity.performCreate(Activity.java:6251)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
           at android.os.Handler.dispatchMessage(Handler.java:102) 
           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) 
4

1 回答 1

1

您正在执行没有 offerId 的父 onCreate 方法。更改以下两行

super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();

对此:

Bundle extras = getIntent().getExtras();
if (savedInstanceState == null) { 
    super.onCreate(extras); 
}
于 2017-12-07T18:01:16.333 回答