1

我正在创建一个简单的 ListView 应用程序来了解更多关于 android 编程的信息。

然而,这个简单的代码(在 Eclipse 中没有显示任何错误)只是在模拟器中启动时崩溃。有任何想法吗?

public class MainActivity extends Activity {

static final String[] FRUITS = { "Apple", "Avocado", "Banana",
    "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
    "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_fruit);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_fruit, FRUITS);

    ListView listView = (ListView) findViewById(R.layout.list_fruit);
    listView.setTextFilterEnabled(true);
    listView.setAdapter(adapter);



    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}

}

list_fruit.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:padding="10dp"
     android:textSize="20sp" >
</TextView>

尝试按照您的说明查找堆栈跟踪,唯一出现的消息是 FATAL EXCEPTION: main.

4

4 回答 4

2

这个:

 ListView listView = (ListView) findViewById(R.layout.list_fruit);

肯定是错误的,因为您需要使用 findViewById 进行查找id,因此例如像这样,但是您需要确保有一个具有该 ID 的 listview 元素:

 ListView listView = (ListView) findViewById(R.id.the_id_of_your_view);
于 2014-03-05T18:38:20.600 回答
0

改变这个

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_fruit, FRUITS);

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

你有

setContentView(R.layout.list_fruit); // list_fruit.xml

你在ArrayAdapter

也改变

ListView listView = (ListView) findViewById(R.layout.list_fruit);

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

也改成

Toast.makeText(getApplicationContext(),FRUITS[postion], Toast.LENGTH_SHORT).show();

编辑:list_fruit.xml没有 ListView。

您需要以下内容list_fruit.xml

<ListView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> 

然后在onCreate

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fruit);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, FRUITS);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setTextFilterEnabled(true);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          Toast.makeText(getApplicationContext(),FRUITS[postion], Toast.LENGTH_SHORT).show();
    }
});
}
于 2014-03-05T18:38:11.523 回答
0

例如,这是错误的:

ListView listView = (ListView) findViewById(R.layout.list_fruit);

它应该是这样的:

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

其中 my_list_fruit 只是 xml 中分配的 android:id 标签:

<ListView
    android:id="@+id/my_list_fruit"
.../>
于 2014-03-05T18:39:02.987 回答
0

试试这个,使用 android simple_list_view_1

public class MainActivity extends Activity {

static final String[] FRUITS = { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fruit);
ListView listView = (ListView) findViewById(R.id.list_fruit);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, FRUITS);


listView.setTextFilterEnabled(true);
listView.setAdapter(adapter);



listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    }
});

}

}

或者在其中定义一个带有 textview 的 list_item.xml,例如:

MainActivity.java

    public class MainActivity extends Activity {

static final String[] FRUITS = { "Apple", "Avocado", "Banana", "Blueberry",
        "Coconut", "Durian", "Guava", "Kiwifruit", "Jackfruit", "Mango",
        "Olive", "Pear", "Sugar-apple" };
private ListView listView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_fruit);
    listView = (ListView) findViewById(R.id.list_fruit);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.list_item, R.id.textView1, FRUITS);

    listView.setTextFilterEnabled(true);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(getApplicationContext(),
                         listView.getItemAtPosition(position).toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });

}}

list_fruit.xml

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/list_fruit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >

</ListView>

</RelativeLayout>

list_item.xml

<?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="horizontal" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
于 2014-03-05T19:07:49.437 回答