3

PlaceActivity 托管两个片段

  1. PlaceDetailsFragment - 有一个视图寻呼机 - PlaceSlideFragment
  2. PlaceMapFramgment - 托管 Google 地图

当我尝试更改方向时,应用程序崩溃并显示以下错误日志。

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

3 回答 3

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

于 2013-01-12T10:06:08.157 回答
3

堆栈跟踪包含以下行

无法实例化片段 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;
    }
}

在寻呼机中

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

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

于 2013-01-12T10:08:05.840 回答
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

这一行表明您没有支持库。添加支持库如下:

右键项目 -> Android 工具 -> 添加支持库

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

于 2013-01-12T10:08:05.157 回答