Android On Orientation改变崩溃

时间:2013-01-12 10:04:15

标签: android android-fragments screen-orientation android-orientation

PlaceActivity托管两个碎片

  1. PlaceDetailsFragment - 具有视图寻呼机 - PlaceSlideFragment
  2. PlaceMapFramgment - 托管Google地图
  3. 当我尝试更改方向时,应用程序崩溃并显示以下错误日志。

    01-12 15:24:57.950: E/AndroidRuntime(30412): FATAL EXCEPTION: main
    01-12 15:24:57.950: E/AndroidRuntime(30412): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m7.nomad/com.m7.nomad.PlaceActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.m7.nomad.fragments.PlaceSlideFragment: make sure class name exists, is public, and has an empty constructor that is public
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3363)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread.access$700(ActivityThread.java:127)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1163)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.os.Handler.dispatchMessage(Handler.java:99)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.os.Looper.loop(Looper.java:137)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread.main(ActivityThread.java:4507)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at java.lang.reflect.Method.invokeNative(Native Method)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at java.lang.reflect.Method.invoke(Method.java:511)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at dalvik.system.NativeStart.main(Native Method)
    01-12 15:24:57.950: E/AndroidRuntime(30412): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.m7.nomad.fragments.PlaceSlideFragment: make sure class name exists, is public, and has an empty constructor that is public
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at com.m7.nomad.PlaceActivity.onCreate(PlaceActivity.java:34)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.Activity.performCreate(Activity.java:4465)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    ... 12 more
    01-12 15:24:57.950: E/AndroidRuntime(30412): Caused by: java.lang.InstantiationException: can't instantiate class com.m7.nomad.fragments.PlaceSlideFragment; no empty constructor
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at java.lang.Class.newInstanceImpl(Native Method)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at java.lang.Class.newInstance(Class.java:1319)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
    01-12 15:24:57.950: E/AndroidRuntime(30412):    ... 19 more
    

    PlaceSlidesFragemntsAdapter.java

    public class PlaceSlidesFragmentAdapter extends FragmentPagerAdapter  {
    
        DBAdapter db = new DBAdapter(SplashActivity.context);
        List<String> photoList = new ArrayList<String>();
    
        private int mCount;
    
        public PlaceSlidesFragmentAdapter(FragmentManager fm) {
            super(fm);
    
            db.open();
            Cursor photosCursor = db.getPhotos(PlaceActivity.placeId);
    
            // Iterate Through the Place Cursor
            if (photosCursor.moveToFirst()) {
                do {
                    photoList.add(photosCursor.getString(1));
                } while (photosCursor.moveToNext());
            }
    
            db.close();
    
            mCount = photoList.size();
        }
    
        @Override
        public Fragment getItem(int position) {
            return new PlaceSlideFragment(photoList.get(position));
        }
    
        @Override
        public int getCount() {
            return mCount;
        }
    
        public void setCount(int count) {
            if (count > 0 && count <= 10) {
                mCount = count;
                notifyDataSetChanged();
            }
        }
    }
    

    PlaceSlideFragment.java

    public final class PlaceSlideFragment extends Fragment {
        String imageResourceId;
    
        public PlaceSlideFragment(String i) {
            imageResourceId = i;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            ImageView image = new ImageView(getActivity());
    
            ImageLoader imgLoader = new ImageLoader(MainActivity.context);
            imgLoader.DisplayImage(imageResourceId, new Activity(), image);
    
            LinearLayout layout = new LinearLayout(getActivity());
            layout.setLayoutParams(new LayoutParams());
    
            layout.setGravity(Gravity.CENTER);
            layout.addView(image);
    
            return layout;
        }
    }
    

3 个答案:

答案 0 :(得分:8)

您的Fragment必须有空的构造函数。

使用setArguments(Bundle)而不是直接将参数传递给构造函数。

PlaceSlideFragment.java删除构造函数的示例中。相反,在onCreateView内使用:

imageResourceId = getArguments().getString("param");

然后,即时PlaceSlideFragment使用此代码:

Fragment f = new PlaceSlideFragment();
Bundle args = new Bundle();
args.putString("param", yourParam);
f.setArguments(args);

或者如果你将这段代码移动到PlaceSlideFragment

的静态方法中会更好

答案 1 :(得分:3)

stacktrace包含以下行

  

无法实例化片段com.m7.nomad.fragments.PlaceSlideFragment:确保类名存在,是公共的,并且具有公共的空构造函数

构造函数存在问题,片段的一个好习惯是使用将调用空构造函数的getInstance方法。

为什么需要空构造函数?

  

Android框架会在重新创建片段时调用这些片段,例如   配置改变

如果您对下面的片段评论有任何其他问题

编辑:

碎片示例

public final class PlaceSlideFragment extends Fragment {

    public static Fragment newInstance() {
      return new PlaceSlideFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ImageView image = new ImageView(getActivity());
        ImageLoader imgLoader = new ImageLoader(MainActivity.context);
        imgLoader.DisplayImage(getArguments().getInt("key"), new Activity(), image);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams());

        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;
    }
}

IN PAGER

 @Override
    public Fragment getItem(int position) {
        PlaceSlideFragment frag = PlaceSlideFragment.newInstance();
        Bundle args = new Bundle()
        args.putInt("key",ID_RESOURCE_HERE);
        frag.setArguments(args);
        return frag;
    }

我希望这会有所帮助,您还可以查看保留片段并处理配置更改,因为您可能会在以后遇到其他异常

答案 2 :(得分:1)

Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.m7.nomad.fragments.PlaceSlideFragment: make sure class name exists, is public, and has an empty constructor that is public

此行显示您没有支持库。 添加支持库如下:

右键单击项目 - &gt; Android工具 - &gt;添加支持库

然后清理并构建您的项目。