Home > Net >  Android App crash when using custom Listview and logcat doesnt show anything
Android App crash when using custom Listview and logcat doesnt show anything

Time:12-03

I'm creating a Listview that shows the names of the days in a week. The app crashes and the logcat doesn't show anything. How to solve this issue?

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 this is the layout for the custom listview

<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>

CodePudding user response:

Credit to @Mike M. Solution:

The LinearLayout should change to TextView. class_timetable_listview_layout.xml should change to

<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);
  • Related