我无法从 2 天开始显示 listview,阅读 android.develop、here 和 google 中的所有线程,但我无法弄清楚我的应用程序崩溃的原因。如果我从 ListShow() 中删除最后一行,则应用程序不会崩溃,但如果我 list.setAdapter(push); 该应用程序根本不启动。
MainActivity.java
public class MainActivity extends Activity {
//Main function
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call all function to create
TextCount();
ListShow();
}
//Menu function
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//Text counter function
public void TextCount() {
int c=0;
TextView t = new TextView(this);
t=(TextView)findViewById(R.id.file_counter);
t.setText((c)+" files loaded");
}
//List Function
public void ListShow() {
String[] items= new String[] {"item 1", "item 2", "item 3", "item 4", "item 5", "item 5"};
ArrayAdapter<String> push = new ArrayAdapter<String>(this, R.layout.item, items);
ListView list = (ListView) findViewById(R.id.list_view);
list.setAdapter(push);
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<include layout="@layout/header" />
<View android:id="@+id/separator_up"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/blue_light"/>
<TextView
android:id="@+id/file_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/file_counter" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<View android:id="@+id/separator_down"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/blue_light"/>
<include layout="@layout/footer" />
</LinearLayout>
项目.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/simple"
android:text="" >
</TextView>