加载ListView片段失败

时间:2014-02-21 04:22:24

标签: android-listview android-fragments fragmenttransaction

我有一个无效的片段事务。我的活动(SearchActivity)显示一个警告对话框片段,当用户单击是或否(正/负)时,在SearchActivity中实现onClickListener。在重写的方法中,我创建了一个片段事务来加载列表片段。但是,当我单击对话框按钮时,我的应用程序崩溃了。错误日志说有一个id ...不是ListView类。但是,我不知道这是从哪里来的。我调用新片段的方式是否正确?任何建议都将不胜感激。

搜索活动覆盖:

    @Override
public void onDialogClick(DialogFragment dialog) {
    ResultListFragment rls = new ResultListFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.searchLayout_container,rls);
    ft.addToBackStack("search");
    ft.commit();
}

对话片段:

public class MoreIngredientsDialogFragment extends DialogFragment {
SearchActivity sActivity;

public interface MoreIngredientsDialogListener{
    public void onDialogClick(DialogFragment dialog);
}

MoreIngredientsDialogListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);ce
    try {
        mListener = (MoreIngredientsDialogListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement MoreIngredientsDialogListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Do you have additional ingredient a?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            mListener.onDialogClick(MoreIngredientsDialogFragment.this);
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            mListener.onDialogClick(MoreIngredientsDialogFragment.this);
        }
    });
    return builder.create();
}
}

列表片段(ResultListFragment):

public class ResultListFragment extends ListFragment {
String[] menuItems = {"beef", "chicken", "pie"};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1, menuItems));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_result, container, false);
    return view;
}
}

错误日志:

02-20 12:29:57.381    2114-2114/com.ygutstein.neiumasterproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
        at android.app.ListFragment.ensureList(ListFragment.java:402)
        at android.app.ListFragment.onViewCreated(ListFragment.java:203)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:842)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
        at android.app.BackStackRecord.run(BackStackRecord.java:622)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

02-20 12:29:57.421     873-1092/system_process W/ActivityManager﹕ Force finishing activity com.ygutstein.neiumasterproject/.SearchActivity
    02-20 12:29:57.431     873-1092/system_process W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21020

1 个答案:

答案 0 :(得分:0)

事实证明我需要在我的xml文件中更改我的ListView。在这里看到答案:android.R.id.list not ListView

原名:

android:id="@+:id/listView"` 

新:

android:id="@android:id/list"