【发布时间】:2014-04-22 14:08:31
【问题描述】:
这是我的第一个 Android 应用程序,我正在尝试添加一个列表视图,并且我遵循了许多教程,但我总是得到相同的结果,启动应用程序时崩溃。
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:background="@drawable/background"
android:gravity="top"
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="com.mattigins.mediacenter.MainActivity$PlaceholderFragment" >
<View
android:id="@+id/view1"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/View01"
android:background="@drawable/semi_trans_box" />
<View
android:id="@+id/View01"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_marginRight="58dp"
android:layout_toLeftOf="@+id/view1"
android:background="@drawable/semi_trans_box" />
<ListView
android:id="@+id/List1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/View01"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/View01" >
</ListView>
代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ListView listView = (ListView) findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);
}
【问题讨论】:
-
请用 logcat ... 还有什么 simple_list_item_1?
-
您是否使用提供的 XML 作为布局?因为您没有
R.id.container..如果您的列表在片段中,您应该将代码移动到片段的onCreateView -
@yogx :
android.R.layout应该告诉你它是什么...... -
作为一个建议,不要在你的 xml 中大写你的 id。
-
查看这个
tools:context="com.mattigins.mediacenter.MainActivity$PlaceholderFragment"可以看出它属于片段布局,尽管这与崩溃无关。发布堆栈跟踪将确认
标签: java android eclipse listview