0

次のフラグメントがあります。デバイスを回転させるまで、完全に正常に動作します。RadarSelectionFragmentその後、失敗に関するエラーでクラッシュしInstantiateます。FragmentPagerAdapter問題のとのコードは次のFragmentとおりです。

@SuppressLint("DefaultLocale")
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter() {
           // Do some stuff     
    }


    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            Fragment fragment = new RadarSelectionFragment();
            Bundle args = new Bundle();
            args.putInt(RadarSelectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        } else if (position == 1) {
            Fragment fragment = new WeatherMapDisplayFragment();
            Bundle args = new Bundle();
            args.putInt(WeatherMapDisplayFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        } else {
            Fragment tf = new RadarSelectionFragment();
            return tf;
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @SuppressLint("DefaultLocale")
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase();
        case 1:
            return getString(R.string.title_section2).toUpperCase();
        }
        return null;
    }
}


public static class RadarSelectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public RadarSelectionFragment() {}

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
            return inflater.inflate(R.layout.radars, container, false);
        } else {
            return container;
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

     @Override
     public void onSaveInstanceState(Bundle outState) {

         super.onSaveInstanceState(outState);
     }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);

        ...
    }

    public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }


} 

public class WeatherMapDisplayFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public WeatherMapDisplayFragment() {
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
            return inflater.inflate(R.layout.display, container, false);
        } else {
            return container;
        }
    }
} 

私は問題をグーグルで調べてみましたが、私が見つけ続けているのは、それが確実であることに関するさまざまな解決策だけFragment Classですstatic(私がやった)。

私は Android プログラミングに比較的慣れていないので、回答があれば、例を投稿するか、時間があれば他の例へのリンクをお願いします。

前もって感謝します!

編集1:スタックトレース

01-21 13:46:31.907: E/AndroidRuntime(1101): FATAL EXCEPTION: main
01-21 13:46:31.907: E/AndroidRuntime(1101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radarau/com.example.radarau.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.radarau.MainActivity$WeatherMapDisplayFragment: make sure class name exists, is public, and has an empty constructor that is public
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.os.Looper.loop(Looper.java:137)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at java.lang.reflect.Method.invoke(Method.java:511)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at dalvik.system.NativeStart.main(Native Method)
01-21 13:46:31.907: E/AndroidRuntime(1101): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.radarau.MainActivity$WeatherMapDisplayFragment: make sure class name exists, is public, and has an empty constructor that is public
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at com.example.radarau.MainActivity.onCreate(MainActivity.java:35)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.Activity.performCreate(Activity.java:5104)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-21 13:46:31.907: E/AndroidRuntime(1101):     ... 12 more
01-21 13:46:31.907: E/AndroidRuntime(1101): Caused by: java.lang.InstantiationException: can't instantiate class com.example.radarau.MainActivity$WeatherMapDisplayFragment; no empty constructor
01-21 13:46:31.907: E/AndroidRuntime(1101):     at java.lang.Class.newInstanceImpl(Native Method)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at java.lang.Class.newInstance(Class.java:1319)
01-21 13:46:31.907: E/AndroidRuntime(1101):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
01-21 13:46:31.907: E/AndroidRuntime(1101):     ... 19 more
4

1 に答える 1

1

あなたのログキャットは言う

Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.radarau.MainActivity$WeatherMapDisplayFragment: make sure class name exists, is public, and has an empty constructor that is public

そのための public Default コンストラクターを提供する必要があります

public WeatherMapDisplayFragment() {
   // Do some stuff     
}

そのクラスがネストされている場合は、それを作成しclassstatic classパブリックのデフォルト コンストラクターを提供するか、そのフラグメントを新しい Java ファイルに移動します。

于 2013-01-21T03:49:04.967 に答える