我在活动的onCreate中实现了一个Map Support片段,如下所示:

mMapFragment = new DRPCustomMapFragment() {

            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                _map = mMapFragment.getMap();
                setUpMapUI();
            }
        };

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.mapContainer, mMapFragment);
        fragmentTransaction.commit();

        fm.executePendingTransactions();


实际的CustomMapFragment类如下所示:

public class DRPCustomMapFragment extends SupportMapFragment {

    private View mOriginalView;
    private DRPMapWrapperLayout mMapWrapperLayout;

    public DRPCustomMapFragment(){

    }

    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            SupportMapFragment fragment = (SupportMapFragment) getActivity()
                                              .getSupportFragmentManager().findFragmentById(
                                                  R.id.map);
            if (fragment != null) getFragmentManager().beginTransaction().remove(fragment).commit();

        } catch (IllegalStateException e) {
            //handle this situation because you are necessary will get
            //an exception here :-(
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mOriginalView = super.onCreateView(inflater, container, savedInstanceState);

        mMapWrapperLayout = new DRPMapWrapperLayout(getActivity());
        mMapWrapperLayout.addView(mOriginalView);

        return mMapWrapperLayout;
}

    @Override
    public View getView() {
        return mOriginalView;
    }

    public void setOnDragListener(DRPMapWrapperLayout.OnDragListener onDragListener) {
        mMapWrapperLayout.setOnDragListener(onDragListener);
    }

}


当我从开发人员选项中打开“不要保留活动”,以测试保存的实例状态包时,出现错误:

> 04-25 07:35:31.362: E/AndroidRuntime(19210):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.sdsd.inhouse.drop/com.sdsd.inhouse.drop.activities.DRPCreateDropActivity}:
> android.support.v4.app.Fragment$InstantiationException: Unable to
> instantiate fragment
> com.sdsd.inhouse.drop.activities.DRPCreateDropActivity$5: make sure
> class name exists, is public, and has an empty constructor that is
> public 04-25 07:35:31.362: E/AndroidRuntime(19210):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.os.Handler.dispatchMessage(Handler.java:102) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> android.os.Looper.loop(Looper.java:136) 04-25 07:35:31.362:
> E/AndroidRuntime(19210):  at
> android.app.ActivityThread.main(ActivityThread.java:5017) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> java.lang.reflect.Method.invokeNative(Native Method) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> java.lang.reflect.Method.invoke(Method.java:515) 04-25 07:35:31.362:
> E/AndroidRuntime(19210):  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> dalvik.system.NativeStart.main(Native Method) 04-25 07:35:31.362:
> E/AndroidRuntime(19210): Caused by:
> android.support.v4.app.Fragment$InstantiationException: Unable to
> instantiate fragment
> com.sdsd.inhouse.drop.activities.DRPCreateDropActivity$5: make sure
> class name exists, is public, and has an empty constructor that is
> public 04-25 07:35:31.362: E/AndroidRuntime(19210):   at
> android.support.v4.app.Fragment.instantiate(Fragment.java:413) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> com.sdsd.inhouse.drop.activities.DRPCreateDropActivity.onCreate(DRPCreateDropActivity.java:183)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.app.Activity.performCreate(Activity.java:5231) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 04-25 07:35:31.362: E/AndroidRuntime(19210):  ... 11 more 04-25
> 07:35:31.362: E/AndroidRuntime(19210): Caused by:
> java.lang.InstantiationException: can't instantiate class
> com.sdsd.inhouse.drop.activities.DRPCreateDropActivity$5; no empty
> constructor 04-25 07:35:31.362: E/AndroidRuntime(19210):  at
> java.lang.Class.newInstanceImpl(Native Method) 04-25 07:35:31.362:
> E/AndroidRuntime(19210):  at
> java.lang.Class.newInstance(Class.java:1208) 04-25 07:35:31.362:
> E/AndroidRuntime(19210):  at
> android.support.v4.app.Fragment.instantiate(Fragment.java:402) 04-25
> 07:35:31.362: E/AndroidRuntime(19210):    ... 18 more


我看到了有关此here的另一篇文章,但坦率地说,我不明白解决方案是什么

最佳答案

解决方案是创建一个真实的子类。

语法new DRPCustomMapFragment() { // stuff }创建一个扩展DRPCustomMapFragment的匿名内部类。它没有构造函数,更不用说公共零参数了。

所以:


创建扩展AnotherDRPCustomMapFragmentDRPCustomMapFragment,将您的逻辑从您的匿名内部类移到那里。
DRPCustomMapFragment中删除​​零参数构造函数,或者至少从中调用super()
AnotherDRPCustomMapFragment用作您的mMapFragment数据成员。

关于android - map 支持上没有空的构造函数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23292203/

10-10 01:14