【问题标题】:App crashes when opening a class which contains a ListView打开包含 ListView 的类时应用程序崩溃
【发布时间】:2012-06-02 10:42:30
【问题描述】:

我有以下类,如您所见,它包含一个 EditText、Button 和列表。 我希望用户在 EditText 中输入一些文本,单击搜索并显示来自

的结果
SQLite database in the list below the button/edittext field.
public class FindOrder  extends ListActivity {
private List<String> arr;
private ListView listView;
Button search;
EditText search_field;
String search_val;
SQLiteDatabase db = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.find_order);
    // TODO Auto-generated method stub
    Log.e("myTag","in func");
    search = (Button) findViewById(R.id.search_button);
    search_field = (EditText) findViewById(R.id.search_field);
    search.setOnClickListener(  
            new View.OnClickListener() {
                public void onClick(View view) {
                    search_val=search_field.getText().toString();
                    //getOrder(search_val);

                }
            });
}

public void getOrder(String val) {
    Log.e("myTag","in func");
    String query;
    String[] s_arr = new String[1];
    listView = (ListView) findViewById(R.id.listView);
    ArrayAdapter<String> adapter;
    arr = new ArrayList<String>();
    db =  this.openOrCreateDatabase( "DBname", MODE_PRIVATE, null);
    query="SELECT OrderName, OrderLink, DateYear, DateMonth, DateDay, OrderPrice FROM Orders where OrderName=?";
    s_arr[0]=val;
    Cursor c = db.rawQuery(query, s_arr);
    if (c != null ) {
       if  (c.moveToFirst()) {
           do {     
               arr.add(c.getString(c.getColumnIndex(DatabaseHelper.colName)));
               Log.e("myTag",c.getString(c.getColumnIndex(DatabaseHelper.colName)));
           } while (c.moveToNext());
       }
   } 
   c.close();
  //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr));
  adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
  listView.setAdapter(adapter);
  TextView textview = new TextView(this);
  LinearLayout ll2 = (LinearLayout)findViewById(R.id.linearLayout2);
  ll2.addView(textview);

}
}

目前,每当我单击主菜单中导致此活动的按钮时,我都会得到一个 FC。 我可以在日志中看到的错误之一是:

06-02 10:32:18.387: E/AndroidRuntime(1904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Sagi.MyOrders/com.Sagi.MyOrders.FindOrder}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

与许多其他错误一起。 但我真的不明白为什么我应该有 .list 属性...

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
    <LinearLayout
        android:id = "@+id/linearLayout1"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <EditText
                android:id="@+id/search_field"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10" >
        </EditText>
        <Button
            android:id="@+id/search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search" />
    </LinearLayout>
    <LinearLayout
        android:id = "@+id/linearLayout2"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </LinearLayout>
    <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

谢谢!

【问题讨论】:

    标签: android class listview forceclose


    【解决方案1】:

    只需在声明 listview 的 xml 文件中更改这一行。

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

    无需声明列表视图。请尝试以下代码。

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
    

    请查看以下示例。

    public class MyListActivity extends ListActivity {
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" };
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);
        }
    }
    

    【讨论】:

    • 那么下面一行应该得到什么:listView = (ListView) findViewById(???)
    • 在扩展 ListActivity 时将其移除。
    • 已删除,无 FC...但未显示列表,正在查询记录...但未显示任何内容。
    • 你有没有从 cursor 得到值?尝试记录该值。
    【解决方案2】:

    当您将 ListActivity 扩展到您的 Activity 时,您的列表视图 ID 必须为 android:id="@android:id/list"

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

    【讨论】:

    • 试过了,如果我在那里改变,我还必须改变 Java 代码,我得到相同的 FC。 XML 中的名称不应更改,因为我对待它...
    • 我不知道这是否已经解决了,但我可能会给出意见:您是否无意添加import android.R
    【解决方案3】:

    在 xml 文件 android:id="@android:id/list" 中更改您的列表视图 ID

    在java代码中,

    你用过listView = (ListView) findViewById(R.id.listView)
    将其更改为listView = this.getListView()

    【讨论】:

      【解决方案4】:

      解决方案 1: 在xml文件中更改listview的id

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

      并且在您的代码中更改此行 listView = (ListView) findViewById(R.id.listView);作为

      listView = (ListView) findViewById(R.id.list);
      

      解决方案 2:

      您的活动必须扩展 Activity 而不是 ListActivity。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        相关资源
        最近更新 更多