我正在创建一个简单的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。
答案 0 :(得分:2)
此:
ListView listView = (ListView) findViewById(R.layout.list_fruit);
肯定是错误的,因为您需要使用findViewById查找id
,例如像这样,但您需要确保拥有带有该id的listview元素:
ListView listView = (ListView) findViewById(R.id.the_id_of_your_view);
答案 1 :(得分: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();
}
});
}
答案 2 :(得分: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"
.../>
答案 3 :(得分: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>