如何将结果的 activity 添加到 android-json-form-wizard?

How to add activity for result to android-json-form-wizard?

我正在使用 Android-Json-Form-Wizard library 构建表单生成器;但是,特定项目需要 startactivityforresult 以 return 返回结果作为项目的答案。

因此,我在某些方面更改了库;因此,当我以前回到 activity 时,我遇到了 NullPointerException。

下面是使用的代码。使用 Intent.ACTION_PICK 的部分工作正常,另一个是问题所在。

String key = (String) v.getTag(R.id.key);
String type = (String) v.getTag(R.id.type);
if (JsonFormConstants.CHOOSE_IMAGE.equals(type)) {
    getView().hideKeyBoard();
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    mCurrentKey = key;
    getView().startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
} else if(JsonFormConstants.DATE_TIME.equals(type)) {
    getView().hideKeyBoard();
    Intent dateTimeIntent = new Intent(getView().getContext(), DateTimeActivity.class);
    mCurrentKey = key;
    getView().startActivityForResult(dateTimeIntent, SELECT_DATE_TIME);
}

错误代码:

    03-17 02:08:41.099 30597-30597/br.com.checkd E/AndroidRuntime: FATAL EXCEPTION: main
    Process: br.com.checkd, PID: 30597
        java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.checkd/com.vijay.jsonwizard.activities.JsonFormActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.vijay.jsonwizard.mvp.ViewState.setSavedInstance(boolean)' on a null object reference
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
           at android.app.ActivityThread.-wrap11(ActivityThread.java)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:148)
           at android.app.ActivityThread.main(ActivityThread.java:5443)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.vijay.jsonwizard.mvp.ViewState.setSavedInstance(boolean)' on a null object reference
           at com.vijay.jsonwizard.mvp.BaseFragment.onCreate(BaseFragment.java:26)
           at com.vijay.jsonwizard.fragments.JsonFormFragment.onCreate(JsonFormFragment.java:65)
           at android.support.v4.app.Fragment.performCreate(Fragment.java:2068)
           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1055)
           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1272)
           at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2144)
           at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:190)
           at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:351)
           at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
           at com.vijay.jsonwizard.activities.JsonFormActivity.onCreate(JsonFormActivity.java:35)
           at android.app.Activity.performCreate(Activity.java:6259)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
           at android.os.Handler.dispatchMessage(Handler.java:102) 
           at android.os.Looper.loop(Looper.java:148) 
           at android.app.ActivityThread.main(ActivityThread.java:5443) 
           at java.lang.reflect.Method.invoke(Native Method) 
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.vijay.jsonwizard.mvp.ViewState.setSavedInstance(boolean)' on a null object reference

基础片段

public abstract class BaseFragment<VS extends ViewState> extends Fragment {

    // @Icicle
    VS mViewState;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Icepick.restoreInstanceState(this, savedInstanceState);
        if (savedInstanceState == null) {
            mViewState = createViewState();
            mViewState.setSavedInstance(false);
        } else {
            mViewState.setSavedInstance(true);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Icepick.saveInstanceState(this, outState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int layoutRes = getLayoutRes();
        if (layoutRes == 0) {
            throw new IllegalArgumentException("getLayoutRes() returned 0, which is not allowed. "
                    + "If you don't want to use getLayoutRes() but implement your own view for this "
                    + "fragment manually, then you have to override onCreateView();");
        } else {
            View v = inflater.inflate(layoutRes, container, false);
            return v;
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * Return the layout resource like R.layout.my_layout
     *
     * @return the layout resource or null, if you don't want to have an UI
     */
    protected int getLayoutRes() {
        return 0;
    }

    protected abstract VS createViewState();

    public VS getViewState() {
        return mViewState;
    }
}

有人可以用它工作吗?

谢谢

尝试始终在使用变量之前对其进行初始化。

public abstract class BaseFragment<VS extends ViewState> extends Fragment {

    // @Icicle
    VS mViewState;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Icepick.restoreInstanceState(this, savedInstanceState);

        if (mViewState == null) mViewState = createViewState();

        mViewState.setSavedInstance(savedInstanceState != null);

    }

顺便说一句,

@return the layout resource or null,

你不能 return null; 作为 int。尝试使方法成为 Integer return 类型,如果你想要 null 支持