碎片 - InstantiationException:没有空的构造函数 - >谷歌地图V2

2019-08-31 10:08发布

我收到此错误信息,当我通过应用程序,更改按钮再次打开封闭的应用:

Caused by: java.lang.InstantiationException: can't instantiate class com.*.FragmentContact$1; no empty constructor

我发现有关内部类中的一些技巧,并让他们静等但这FragmentContact是在*的.java文件一个公共类和有一个公共的空构造 。 我使用谷歌地图API V2在这个项目,并从互联网上设置我的MapView做一招地方。 看这里:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_contact, null);

    try {
        MapsInitializer.initialize(this.getActivity().getApplicationContext());
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }

    fragment = new SupportMapFragment() {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mMap = fragment.getMap();
            if (mMap != null) {
                setupMap();
            }
        }
    };

    getFragmentManager().beginTransaction().replace(R.id.fragment_orte_map_parent, fragment).commit();

    return v;
}

当我剪这个MapView类,事情出一切工作正常。 也许有人可以解释我做错了。

我完整的堆栈跟踪:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*/com.*.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.*.FragmentContact$1: make sure class name exists, is public, and has an empty constructor that is public
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
   at android.app.ActivityThread.access$600(ActivityThread.java:153)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5226)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.*.FragmentContact$1: make sure class name exists, is public, and has an empty constructor that is public
   at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
   at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
   at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
   at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
   at com.*.MainActivity.onCreate(MainActivity.java:20)
   at android.app.Activity.performCreate(Activity.java:5104)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
   ... 11 more
Caused by: java.lang.InstantiationException: can't instantiate class com.*.FragmentContact$1; no empty constructor
   at java.lang.Class.newInstanceImpl(Native Method)
   at java.lang.Class.newInstance(Class.java:1319)
   at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
   ... 18 more

Answer 1:

但这些FragmentContact是在*的.java文件一个公共类和有一个公共的空构造。

该错误不抱怨FragmentContact 。 据抱怨第一内部类的FragmentContactFragmentContact$1 )。 你不能有一个Fragment作为一个内部类的东西来实现,因为它不能从外部类的外部实例化。 一个static内部类是罚款。



Answer 2:

我有这个问题了好半天,终于在这里的评论已经解决了这个问题对我来说。 关于尼斯信息$1的含义匿名内部类。

将您的地图初始化到一个新的公共类,如MyMapFragment.java 。 这将允许在没有的情况下被实例化片段

对于那些仍然不知道如何解决它。 写的代码如下(使用原来的问题的示例代码):

//Existing code to instantiate map
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_contact, null);

    try {
        MapsInitializer.initialize(this.getActivity().getApplicationContext());
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }

    fragment = new MyMapFragment();

    //added callback for catching when the map has finished loading
    mapFragment.setLoadedCallback(new MapLoadedCallback() {
        @Override
        public void onLoaded(GoogleMap map) {
            mMap = map;
        }
    });

    getFragmentManager().beginTransaction().replace(R.id.fragment_orte_map_parent, fragment).commit();

    return v;
}
....

创建新MyMapFragment.java ,使其不再是一个内部类。 这将允许在没有它的外部类中创建的片段。

public class MyMapFragment extends SupportMapFragment 
{
    GoogleMap mMap;
    public MyMapFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
         mMap = fragment.getMap();
         if (mMap != null) {
             mCallback.onLoaded(mMap);
        }
    }
}    

创建MapLoadedCallback.java让你处理时,地图加载完成,并检索加载的地图对象的实例

public interface MapLoadedCallback {
    void onLoaded(GoogleMap map);
}


Answer 3:

  private Entity mEntity;
  public YourFragment() {}
  public static YourFragment getInstance(Entity mEntity) {
    YourFragment fragment = new YourFragment();
    fragment.mEntity = mEntity;
    return fragment;
  }

当你使用这个片段,只需调用静态方法getInstance



文章来源: Fragment - InstantiationException: no empty Constructor -> Google Maps v2?