0

我知道我的代码很可能没有遵循最佳实践。我是一个自称“复制和粘贴”的编码员,并且仍在学习为 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
4

0 回答 0