当我通过应用程序更改按钮再次打开关闭的应用程序时,我收到了此错误消息:
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的东西时,一切都很好。也许有人能解释我做错了什么。
我的完整Stacktrace:
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是*.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;
}
....
创建新的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);
}
-
理想情况下,我们不应该在片段构造函数中传递任何东西,片段构造函数应该是空的或默认的。
-
现在第二个问题是,如果我们想传递接口变量或参数-,该怎么办
- a。我们应该使用Bundle来传递数据
- b。对于Interface,我们可以将parceble放入bundle中,并使该接口实现parceble
- c。如果可能的话,我们可以在活动中实现该接口,在片段中,我们可以初始化OnAttach中的监听器,其中我们有context[(context(listener]
因此,在配置更改期间,例如字体更改,活动娱乐侦听器不会取消初始化,我们可以避免空指针异常。
private Entity mEntity;
public YourFragment() {}
public static YourFragment getInstance(Entity mEntity) {
YourFragment fragment = new YourFragment();
fragment.mEntity = mEntity;
return fragment;
}
当您使用这个片段时,只需调用静态方法getInstance
。