【发布时间】:2021-01-28 21:28:23
【问题描述】:
我在教程的帮助下构建了一个 android 应用程序。
这是 MainActivity.java:-
package com.example.listdisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X", "PHP", "Java", "HTML", "CSS", "JavaScript","MySQL"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
这是activity_main.xml:-
<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">
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这是activity_listview.xml:-
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dip"
android:textSize="16dip"
android:textStyle="bold"
android:orientation="horizontal">
</TextView>
当我将 textview 更改为任何其他视图时,例如 cardview 等等。该应用程序将崩溃。 我想用卡片视图和布局做更多的样式。就像我也尝试过使用线性布局一样。我在线性布局中插入了这个带有按钮的文本视图。但是,它仍然会崩溃。
请帮帮我。
【问题讨论】:
-
使用 CardView 使用 RecycleView 代替 ListView
-
请分享文字崩溃
-
在没有堆栈跟踪的情况下很难调试崩溃。请参阅Unfortunately MyApp has stopped. How can I solve this? 获取特定于 Android 的建议,并查看 What is a stack trace, and how can I use it to debug my application errors? 获取有关在获得堆栈跟踪后该怎么做的建议。如果您仍然需要帮助,请编辑您的问题以包含完整的堆栈跟踪,以及您的代码的哪一行堆栈跟踪指向。
标签: android android-layout android-listview android-arrayadapter