gpt4 book ai didi

java - Android 应用程序崩溃并出现错误 : Resource ID #0x7f0d0061 type #0x12 is not valid

转载 作者:行者123 更新时间:2023-11-30 01:40:36 27 4
gpt4 key购买 nike

我正在尝试使用带有 SimpleCursorAdaptor 的 ListView 来显示数据库记录。该应用程序不断崩溃并出现以下错误:资源 ID #0x7f0d0061 类型 #0x12 无效。下面是崩溃的 Java 代码

public class ViewActivity extends AppCompatActivity  {

DatabaseHelper myDb;
ListView myList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);

myDb = new DatabaseHelper(this);
myList = (ListView) findViewById(R.id.listViewLocations);


getListFromDb();
}

public void getListFromDb(){
Cursor res = myDb.ViewAll();
startManagingCursor(res);


//Map cursor from db to viewFields
String[] fromFieldNames = new String[]{DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4, DatabaseHelper.COL_5};
int[] toViewIDS = new int[]{R.id.viewName, R.id.viewAddress, R.id.viewPostcode, R.id.viewType};

//Create adaptor to map items from DB to UI
SimpleCursorAdapter myCursorAdaptor = new SimpleCursorAdapter(this, R.id.item_layout, res, fromFieldNames, toViewIDS, 0);

// Set adaptor for listView
myList.setAdapter(myCursorAdaptor);

}

两个 XML 文件:activity_view.xml

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewLocations"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

item_layout.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/viewName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/viewAddress"
android:layout_below="@+id/viewName"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/viewPostcode"
android:layout_below="@+id/viewAddress"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/viewType"
android:layout_below="@+id/viewPostcode"
android:layout_alignParentStart="true" />

为什么它总是崩溃,我该怎么做才能修复它。

干杯

最佳答案

item_layout 应该是 layout 而不是 id 请更改如下:

SimpleCursorAdapter myCursorAdaptor = new SimpleCursorAdapter(this, R.layout.item_layout, res, fromFieldNames, toViewIDS, 0);

关于java - Android 应用程序崩溃并出现错误 : Resource ID #0x7f0d0061 type #0x12 is not valid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34561589/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com