问题描述
当我通过应用程序更改按钮再次打开关闭的应用程序时,我收到此错误消息:
I get this error message, when I open a closed App again via App-Change button:
Caused by: java.lang.InstantiationException: can't instantiate class com.*.FragmentContact$1; no empty constructor
我发现了一些关于内部类并使它们成为静态等的技巧.但是这个 FragmentContact 是 *.java-file 和 public 中的一个public 类strong>有一个公共的空构造函数.我在这个项目中使用了 Google Maps Api v2,并在互联网上的某个地方做了一个技巧来设置我的 MapView.看这里:
I've found several tips about Inner-Classes and to make them static etc. But this FragmentContact is a public class in a *.java-file and has a public empty constructor.I'm using Google Maps Api v2 in this project and do a trick somewhere from the internet to setup my MapView. Look here:
@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 的东西时,一切正常.也许有人可以解释我做错了什么.
When I cut this MapView-thing out everything work fine. Maybe someone can explain what I'm doing wrong.
我完整的堆栈跟踪:
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
推荐答案
错误不是抱怨 FragmentContact
.它抱怨 FragmentContact
(FragmentContact$1
) 的第一个内部类.你不能将 Fragment
实现为任何东西的内部类,因为它不能从外部类的外部实例化.static
内部类很好.
The error is not complaining about FragmentContact
. It is complaining about the first inner class of FragmentContact
(FragmentContact$1
). You cannot have a Fragment
implemented as an inner class of anything, as it cannot be instantiated from outside the outer class. A static
inner class is fine.
这篇关于片段 - 实例化异常:没有空的构造函数 ->谷歌地图 v2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!