片段 – InstantiationException:没有空的构造函数 – >谷歌地图v2?

我收到这个错误信息,当我通过应用程序更改button再次打开一个封闭的应用程序:

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

我发现了一些关于Inner-Classes的提示,并且使它们成为静态的。但是这个FragmentContact是一个* .java文件中公共类,并且有一个公共的空构造函数 。 我在这个项目中使用Google Maps 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的东西,一切工作正常。 也许有人可以解释我做错了什么。

我完整的Stacktrace:

 04-17 16:15:22.115: E/AndroidRuntime(22182): FATAL EXCEPTION: main 04-17 16:15:22.115: E/AndroidRuntime(22182): 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 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.ActivityThread.access$600(ActivityThread.java:153) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.os.Handler.dispatchMessage(Handler.java:99) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.os.Looper.loop(Looper.java:137) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.ActivityThread.main(ActivityThread.java:5226) 04-17 16:15:22.115: E/AndroidRuntime(22182): at java.lang.reflect.Method.invokeNative(Native Method) 04-17 16:15:22.115: E/AndroidRuntime(22182): at java.lang.reflect.Method.invoke(Method.java:511) 04-17 16:15:22.115: E/AndroidRuntime(22182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 04-17 16:15:22.115: E/AndroidRuntime(22182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 04-17 16:15:22.115: E/AndroidRuntime(22182): at dalvik.system.NativeStart.main(Native Method) 04-17 16:15:22.115: E/AndroidRuntime(22182): 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 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.support.v4.app.Fragment.instantiate(Fragment.java:405) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208) 04-17 16:15:22.115: E/AndroidRuntime(22182): at com.*.MainActivity.onCreate(MainActivity.java:20) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.Activity.performCreate(Activity.java:5104) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) 04-17 16:15:22.115: E/AndroidRuntime(22182): ... 11 more 04-17 16:15:22.115: E/AndroidRuntime(22182): Caused by: java.lang.InstantiationException: can't instantiate class com.*.FragmentContact$1; no empty constructor 04-17 16:15:22.115: E/AndroidRuntime(22182): at java.lang.Class.newInstanceImpl(Native Method) 04-17 16:15:22.115: E/AndroidRuntime(22182): at java.lang.Class.newInstance(Class.java:1319) 04-17 16:15:22.115: E/AndroidRuntime(22182): at android.support.v4.app.Fragment.instantiate(Fragment.java:394) 04-17 16:15:22.115: E/AndroidRuntime(22182): ... 18 more 

Solutions Collecting From Web of "片段 – InstantiationException:没有空的构造函数 – >谷歌地图v2?"

但是这些FragmentContact是* .java文件中的公共类,并且具有一个公共的空构造函数。

该错误不是抱怨FragmentContact 。 它抱怨FragmentContact的第一个内部类( FragmentContact$1 )。 你不能将Fragment实现为任何内部类,因为它不能从外部类的外部实例化。 static内部类是好的。

我已经有这个问题很多年了,最后这里的评论已经为我解决了。 有关$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; } .... 

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

 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); } } } 

创buildMapLoadedCallback.java以允许您处理地图何时完成加载并检索加载的地图对象的实例

 public interface MapLoadedCallback { void onLoaded(GoogleMap map); } 
  private Entity mEntity; public YourFragment() {} public static YourFragment getInstance(Entity mEntity) { YourFragment fragment = new YourFragment(); fragment.mEntity = mEntity; return fragment; } 

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