我的应用冻结并显示错误您的内容必须有listview

时间:2013-12-28 14:17:39

标签: java android

我想从sqllite数据库中检索数据当我点击类别我的应用程序freez并显示错误你的内容必须有listView,其id为R.id.list这里是我的logcat报告 首先我点击类别报告按钮它在列表视图中显示列表视图我点击类别名称,如“食物”,食物字符串文本按意图传递到另一个活动,保存到光标并传递argumnt到方法调用

12-28 18:48:26.168: E/AndroidRuntime(325): FATAL EXCEPTION: main
12-28 18:48:26.168: E/AndroidRuntime(325): java.lang.RuntimeException: Unable to start 
activity ComponentInfo{com.exmang/com.exmang.ShowCat}: 
java.lang.RuntimeException: Your content must have a ListView whose id attribute is  
'android.R.id.list'
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 18:48:26.168: E/AndroidRuntime(325):  at   
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
android.os.Handler.dispatchMessage(Handler.java:99)
12-28 18:48:26.168: E/AndroidRuntime(325):  at android.os.Looper.loop(Looper.java:123)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
java.lang.reflect.Method.invokeNative(Native Method)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
java.lang.reflect.Method.invoke(Method.java:521)
12-28 18:48:26.168: E/AndroidRuntime(325):  at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

这是我的数据库表声明

public static final String EX_RowID = "_id";
public static final String EX_Cattype = "Ecattype";
public static final String EX_Date = "Ecdate";
public static final String EX_Price = "Ecprice";
public static final String EX_Type = "itype";

private static final String DATABASE_NAME = "DataBase";
private static final int DATABASE_VERSION = 1 ;
private static final String Food_TABLE = "FoodTable";

private DbHelper ourhelper;
private final Context ourcontext;
private SQLiteDatabase ourdatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + Food_TABLE +"(" +
                EX_RowID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                EX_Cattype + " TEXT NOT NULL, " +
                EX_Date + " TEXT NOT NULL," +
                EX_Price + " INTEGER NOT NULL," +
                EX_Type + " TEXT NOT NULL UNIQUE);"
                );

这里是调用字符串值包含每个类别的文本的方法,该文本将等于所需的列

//return category wise data in listview
public Cursor CategExpData(String value) {
// TODO Auto-generated method stub
String[] columns = new String[] {EX_RowID,EX_Cattype, EX_Date, EX_Price, EX_Type };
Cursor c= ourdatabase.query(Food_TABLE, columns, EX_Cattype + "='" + value + "'",  
null, null, null, null);
if (c != null) {

       c.moveToFirst();

      }
return c;

}

这是当我点击listview项目时列表视图的列表视图,列表视图的欲望文本项被传递给另一个活动时

public class Catrpt extends ListActivity {

String myexpenselist[] = {"Food", "Medical" ,"Education",  
"Shopping","Entertainment",
        "Bills", "Automobiles", "Travel"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Catrpt.this, 
 android.R.layout.simple_list_item_1, myexpenselist));

}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    Object o = l.getItemAtPosition(position);
    String str_text = o.toString();
    //Log.i("lsr", str_text.toString());
    Intent intent = new Intent(Catrpt.this, ShowCat.class);
    intent.putExtra("StrValue", str_text);
    Log.i("lsr2", str_text.toString());
    startActivity(intent);
}


 }

现在在下面的类中,所需的数据被保存到光标,它在数据库方法中也匹配到下面的活动中的所需列我的logcat显示所需的值更正我设置的位置以及在此类中我有错误在线的的setContentView(R.layout.myelst);这和ListView listView =(ListView)findViewById(R.id.listView1);
listView.setAdapter(dataAdapter); //将适配器分配给ListView

public class ShowCat extends ListActivity {
    private ExMgDB CatDb;
    private SimpleCursorAdapter dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myelst);

        CatDb = new ExMgDB(this);
        CatDb.open();
        displayListView();
        CatDb.close();
    }
    private void displayListView() {
        // TODO Auto-generated method stub

        String value = getIntent().getStringExtra("StrValue").toString();
        Log.i("lsrval", value.toString());
        Cursor cursor = CatDb.CategExpData(value);
        Log.i("Mytagcrsr", cursor.toString());
        // The desired columns to be bound
        String[] columns = new String[] {
            ExMgDB.EX_RowID,
            ExMgDB.EX_Cattype,
            ExMgDB.EX_Date,
            ExMgDB.EX_Price,
            ExMgDB.EX_Type
        };

        Log.i("Mytagcat", columns.toString());

        int[] to =  new int[]{
            R.id.txtexid,
            R.id.txtexcat,
            R.id.txtexdate,
            R.id.txtexprice,
            R.id.txtexitype
        };
        Log.i("Mytagto", to.toString());
        dataAdapter = new SimpleCursorAdapter(
                    this, R.layout.emtolst, 
                    cursor, 
                    columns, 
                    to,
                    0);

        Log.i("Mytagadap", cursor.toString());

        ListView listView = (ListView) 
        findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);
        Log.i("lsttag", listView.toString());
    }
}

这是myelst xml文件,用于显示listview中的数据

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

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

这里是用于listView

中的显示数据的emtolst xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/foodtablelayout"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:paddingRight="90dp"
    android:text="ID: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:paddingRight="30dp"
    android:text="Category Type: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignRight="@+id/textView2"
      android:layout_below="@+id/textView2"
      android:paddingRight="72dp"
      android:text="Date: "
      android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignRight="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:paddingRight="95dp"
    android:text="Price: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:layout_toLeftOf="@+id/txtexprice"
    android:paddingRight="60dp"
    android:text="Item Type: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/txtexid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_toRightOf="@+id/textView1"
    android:text="TextView" />

 <TextView
    android:id="@+id/txtexcat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_toRightOf="@+id/textView2"
    android:text="TextView" />

<TextView
    android:id="@+id/txtexdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_toRightOf="@+id/textView3"
    android:text="TextView" />

<TextView
    android:id="@+id/txtexprice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView4"
    android:layout_alignBottom="@+id/textView4"
    android:layout_toRightOf="@+id/textView4"
    android:text="TextView" />

<TextView
    android:id="@+id/txtexitype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView5"
    android:layout_alignBottom="@+id/textView5"
    android:layout_toRightOf="@+id/textView5"
    android:text="TextView" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在xml文件中,将ListView的id更改为“@android:id / list”。该计划将正常运作。希望它有所帮助。 :)