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