地图支持上没有空构造函数错误

时间:2014-04-25 11:47:10

标签: android google-maps android-fragments supportmapfragment onrestoreinstancestate

我有一个Map Support片段,我在我的onCreate of activity中实现,如下所示:

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的帖子,但坦率地说,我不明白解决方案是什么

1 个答案:

答案 0 :(得分:2)

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

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

所以:

  • 创建扩展AnotherDRPCustomMapFragment的{​​{1}},将您的逻辑从匿名内部类转移到那里。

  • DRPCustomMapFragment移除零参数构造函数,或者至少从中调用DRPCustomMapFragment

  • super()数据成员使用AnotherDRPCustomMapFragment