【问题标题】:Error dealing with Fragment List View, Cannot find symbol 'list'处理片段列表视图时出错,找不到符号“列表”
【发布时间】:2014-10-22 21:03:14
【问题描述】:

因此,我正在将在所有活动环境中工作的代码移动到使用一个或两个活动以及一堆片段的代码。我现在遇到的问题是我正在尝试转换一个活动,该活动从网络获取一堆基于 JSON 文本的对象,将它们保存到本地 SQLite DB,然后查询它们并将它们放入 ListView。移植 JSON SQLite 的东西并不太难,但我遇到了列表视图的问题......特别是在 findViewById 行,它“找不到符号'list'”

这是我的 onCreateView 代码:

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

    dbHelper.open();
    Cursor cursor = dbHelper.fetchAllTestObjects();
    dbHelper.close();

    // The desired columns to be bound
    String[] columns = new String[]{
            SQLiteDbAdapter.test_object_id,
    };

    // the XML defined views which the data will be bound to
    int[] to = new int[]{
            R.id.test_object_id,
    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
            getActivity(), R.layout.home_test_objects_info,
            cursor,
            columns,
            to,
            0);

    listView = (ListView) view.findViewById(R.id.list);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view,
                                int position, long id) {
            // Get the cursor, positioned to the corresponding row in the result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);

            int testObjectId = cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteDbAdapter.test_object_id));
            DynamoDBManager.CoreInfo listCore = new DynamoDBManager.CoreInfo(testObjectId, "", "");
            // Do action based on listCore
        }
    });

    return view;
}

对于我的 fragment_event_list.xml,我有:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.navigationdrawerexample.EventFragment">    

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

</FrameLayout>

对于将填充 ListView 的对象的对象设置,home_events_info.xml 将在某些时候包含其他 TextView 项:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/eventsRelLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip">

    <TextView
        android:id="@+id/test_object_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</RelativeLayout>

我的活动代码与此代码之间的主要区别在于我使用了片段(列表)模板,它自动将 fragment_event_list.xml 的 id 命名为 list 而不是我在 activity_home.xml 中的 listview1 并且没有使用 LinearLayout 但 FrameLayout。

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

我想这些差异可以解释我当前的问题,因为 Fragment 和 FrameLayout 的工作方式与 Activity 和 LinearLayout 不同,但我不确定如何/为什么以及如何修复错误。

【问题讨论】:

    标签: java android xml android-fragments android-listview


    【解决方案1】:

    我认为您的 Listview 的 id 属性未在 xml 上正确设置

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

    对于:android:id="@+id/list"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多