我理解我的代码很可能不会遵循最佳做法。我是一个自我承认的“复制和粘贴”编码器,我仍然在学习正确的Android代码编码方式,所以请随意将代码翻译成位(如果你有时间和耐心!)。
我创建了一个应用程序,它在应用程序中使用选项卡进行导航,每个应用程序都包含一个片段。每当我去景观时,我都会遇到Fragment$InstantiationException: Unable to instantiate fragment com.jawright.cruisespeed.MainActivity$detailed: make sure class name exists, is public, and has an empty constructor that is public
错误。做了一些阅读后,我发现了一些潜在的问题,首先是片段需要是静态的。但是,如果我使我的片段静态,那么我会得到更多错误; Non-static field 'mContainerView' cannot be referenced from a static context
。我还读过我应该有一个空的构造函数,但我不知道如何为我的例子创建一个。
片段代码:
public class detailed extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContainerView = (ViewGroup)findViewById(R.id.container);
return inflater.inflate(R.layout.detailed_nodata, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContainerView = (ViewGroup) findViewById(R.id.container);
}
}
另一个片段示例代码(我想象的坏编码的好例子):
public class results extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View summaryFragView = inflater.inflate(R.layout.summary, container, false);
lblMean = (TextView)summaryFragView.findViewById(R.id.lblMean);
lblMin = (TextView)summaryFragView.findViewById(R.id.lblMin);
lblMax = (TextView)summaryFragView.findViewById(R.id.lblMax);
lblPerc85 = (TextView)summaryFragView.findViewById(R.id.lblPerc85);
lblPerc15 = (TextView)summaryFragView.findViewById(R.id.lblPerc15);
lblPerc5 = (TextView)summaryFragView.findViewById(R.id.lblPerc5);
lblSD = (TextView)summaryFragView.findViewById(R.id.lblSD);
lblMeanSpeed = (TextView)summaryFragView.findViewById(R.id.lblMeanSpeed);
lblMinSpeed = (TextView)summaryFragView.findViewById(R.id.lblMinSpeed);
lblMaxSpeed = (TextView)summaryFragView.findViewById(R.id.lblMaxSpeed);
lblPerc85Speed = (TextView)summaryFragView.findViewById(R.id.lblPerc85Speed);
lblPerc15Speed = (TextView)summaryFragView.findViewById(R.id.lblPerc15Speed);
lblPerc5Speed = (TextView)summaryFragView.findViewById(R.id.lblPerc5Speed);
lblSDSpeed = (TextView)summaryFragView.findViewById(R.id.lblSDSpeed);
spinnerSpeedDisplay = (Spinner)summaryFragView.findViewById(R.id.spinnerSpeedDisplay);
spinnerSpeedDisplay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
setSpeedType();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
return summaryFragView;
}
}
如果还有更多的代码需要正确解决这个问题,我会很乐意发布它,但不想用不必要的代码淹没帖子。
尝试去景观时,这是logcat:
10-30 23:04:15.428 22587-22587/com.jawright.cruisespeed E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jawright.cruisespeed/com.jawright.cruisespeed.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.jawright.cruisespeed.MainActivity$siteinfo: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.jawright.cruisespeed.MainActivity$siteinfo: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1783)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at com.jawright.cruisespeed.MainActivity.onCreate(MainActivity.java:185)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 12 more
Caused by: java.lang.InstantiationException: can't instantiate class com.jawright.cruisespeed.MainActivity$siteinfo; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
... 19 more