0

I understand that my code most likely does not follow best practices. I am a self confessed "copy and paste" coder and am still learning the proper ways to code for Android so please feel free to rip the code to bits (if you have the time and patience!).

I have made an app which uses tabs for navigation in the app, each of which contains a fragment. Whenever I go landscape I get a 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 error. Having done some reading I found a few potential problems, firstly that the fragment needs to be static. However if I make my fragment static I then get more errors; Non-static field 'mContainerView' cannot be referenced from a static context. I have also read that I should have an empty constructor but I don't know how to create one for my example.

Fragment code:

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); 
        }
    } 

And another fragment example code (good example of bad coding here I imagine):

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;
        }
    }

If there is anymore code needed to properly solve this problem I will happily post it but didn't want to swamp the post with unnecessary code.

Here is logcat when trying to go landscape:

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
Jonny Wright
  • 1,119
  • 4
  • 20
  • 38

0 Answers0