我正在尝试使用带有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" />
为什么它会一直崩溃,我该怎么做才能解决它。
干杯
答案 0 :(得分:1)
item_layout应该是布局不是id请更改如下:
SimpleCursorAdapter myCursorAdaptor = new SimpleCursorAdapter(this, R.layout.item_layout, res, fromFieldNames, toViewIDS, 0);