其他分享
首页 > 其他分享> > Android – ListFragment中的getListView()错误

Android – ListFragment中的getListView()错误

作者:互联网

我在我的应用程序中使用ListFragment并且我想使它为列表中的每个项目都有一个OnItemClick侦听器但是当我添加ListView时lv = getListView();我收到一个错误.如果我删除了listview的工作但是那时我的应用程序将不需要onclick监听器.

以下是我的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

//...       

    ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);

    ListView lv = getListView();
//HERE IS WHERE I AM GETTING THE ERROR

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getActivity().getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);
            in.putExtra(KEY_DESC, description);
            startActivity(in);

        }
    });

    return super.onCreateView(inflater, container, savedInstanceState);
}

Logcat在下面

08-01 23:24:47.152: E/AndroidRuntime(18883): FATAL EXCEPTION: main
08-01 23:24:47.152: E/AndroidRuntime(18883): java.lang.IllegalStateException: Content view not yet created
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.OptimusApps.stayhealthy.AndroidXMLParsingActivity.onCreateView(AndroidXMLParsingActivity.java:74)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:438)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Handler.handleCallback(Handler.java:605)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Looper.loop(Looper.java:137)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at dalvik.system.NativeStart.main(Native Method)

谢谢

解决方法:

在使用之前,您需要打开视图.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.list, container, false);
            // Set adapter etc

        return v;
    }

布局list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="2dp"
    android:paddingRight="2dp" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

    <ProgressBar
        android:id="@id/android:empty"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
         />

</RelativeLayout>

使用onListItemClick:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
  //Responding to clicks
}

标签:android,listview,fragment,android-listfragment
来源: https://codeday.me/bug/20190718/1492621.html