I am creating a simple ListView app to learn more about programming for android.

However, this simple code (which is showing no errors whatsoever in Eclipse) is simply crashing on startup in the emulator. Any ideas?

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>

Tried to find the stacktrace following your instructions, the only message that ame up was FATAL EXCEPTION: main.

有帮助吗?

解决方案 2

Change this

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

to

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

You have

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

and you use the same in the ArrayAdapter

Also change

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

to

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

Also change to

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

Edit: list_fruit.xml does not have a ListView.

You need the below in 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" /> 

Then in 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();
    }
});
}

其他提示

This:

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

is wrong for sure, because you need to look up id with findViewById, so for example like this, but you need to be sure about having a listview element with that id then:

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

For instance, this is wrong:

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

it should be something like:

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

where my_list_fruit is just the android:id tag assigned in the xml:

<ListView
    android:id="@+id/my_list_fruit"
.../>

Try this, using 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();
    }
});

}

}

OR define a list_item.xml with textview in it ex:

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top