Trying to display a table in new activity. New activity worked correctly, but started crashing emulator when I made the table code, please help. There’s no error code so I don’t see the problem.
Attempting to grab the double array of grades in the main class, and display a table with the header Subjects and the columns the names of the students.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
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:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".launchTable">
</TableLayout>
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class launchTable extends AppCompatActivity {
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
String[] subjects= {"Science", "Math", "English"};
String[] students= {"Sarah", "John", "Emily", "Dave"};
double[][] grades= MainActivity.grades;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_table);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
TableLayout tableLayout = findViewById(R.id.table_layout);
for(int i = 0; i < grades.length; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < grades[i].length; j++) {
TextView textView = new TextView(this);
textView.setText(String.valueOf(grades[i][j]));
row.addView(textView);
}
tableLayout.addView(row);
}
}
}
2
I was able to fix it, the emulator didn’t like the line TableLayout tableLayout = findViewById(R.id.table_layout)
so I changed the xml file to a linear layout and made a table inside that