Android 使用自定义 Listview 时应用程序崩溃并且 logcat 不显示任何内容
Android App crash when using custom Listview and logcat doesnt show anything
我正在创建一个列表视图来显示一周中的每一天的名称。应用程序崩溃并且 logcat 不显示任何内容。如何解决这个问题?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv_classTimetable = (ListView) findViewById(R.id.lv_classTimetable);
ArrayList<String> lvDays = new ArrayList<>();
lvDays.add("Monday");
lvDays.add("Tuesday");
lvDays.add("Wednesday");
lvDays.add("Thursday");
lvDays.add("Friday");
lvDays.add("Saturday");
lvDays.add("Sunday");
ArrayAdapter classListAdapter = new ArrayAdapter(MainActivity.this, R.layout.class_timetable_listview_layout, lvDays);
lv_classTimetable.setAdapter(classListAdapter);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv_classTimetable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:dividerHeight="5dp"
android:divider="#d1d1d1"></ListView>
</RelativeLayout>
class_timetable_listview_layout.xml 这是自定义列表视图的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:id="@+id/lv_day"
android:gravity="center"
android:textAlignment="center"
android:text="Day"
android:textStyle="bold"
android:textSize="35sp"
android:layout_width="match_parent"
android:layout_height="60dp"/>
</LinearLayout>
归功于@Mike M。解决方案:
LinearLayout 应更改为 TextView。 class_timetable_listview_layout.xml 应该改为
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="76dp"
android:layout_margin="10dp"
android:gravity="center"
android:textAlignment="center"
android:text="Day"
android:textStyle="bold"
android:textSize="35sp">
</TextView>
MainActivity.java
ArrayAdapter classListAdapter = new ArrayAdapter(this, R.layout.class_timetable_listview_layout, lvDays );
lv_classTimetable.setAdapter(classListAdapter);
我正在创建一个列表视图来显示一周中的每一天的名称。应用程序崩溃并且 logcat 不显示任何内容。如何解决这个问题?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv_classTimetable = (ListView) findViewById(R.id.lv_classTimetable);
ArrayList<String> lvDays = new ArrayList<>();
lvDays.add("Monday");
lvDays.add("Tuesday");
lvDays.add("Wednesday");
lvDays.add("Thursday");
lvDays.add("Friday");
lvDays.add("Saturday");
lvDays.add("Sunday");
ArrayAdapter classListAdapter = new ArrayAdapter(MainActivity.this, R.layout.class_timetable_listview_layout, lvDays);
lv_classTimetable.setAdapter(classListAdapter);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv_classTimetable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:dividerHeight="5dp"
android:divider="#d1d1d1"></ListView>
</RelativeLayout>
class_timetable_listview_layout.xml 这是自定义列表视图的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:id="@+id/lv_day"
android:gravity="center"
android:textAlignment="center"
android:text="Day"
android:textStyle="bold"
android:textSize="35sp"
android:layout_width="match_parent"
android:layout_height="60dp"/>
</LinearLayout>
归功于@Mike M。解决方案:
LinearLayout 应更改为 TextView。 class_timetable_listview_layout.xml 应该改为
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="76dp"
android:layout_margin="10dp"
android:gravity="center"
android:textAlignment="center"
android:text="Day"
android:textStyle="bold"
android:textSize="35sp">
</TextView>
MainActivity.java
ArrayAdapter classListAdapter = new ArrayAdapter(this, R.layout.class_timetable_listview_layout, lvDays );
lv_classTimetable.setAdapter(classListAdapter);