D
D
DeNissss44442020-10-29 23:06:39
Android
DeNissss4444, 2020-10-29 23:06:39

Why does the application just crash when launching the second activity?

Guys, I beg you to help or at least explain to me why, when switching to the second activity, the application can crash? What should be done to find the cause?
And it’s not difficult for anyone who can explain with my example what’s wrong with me, that when switching to the second activity, the application crashes. I wrote a program that works with the SQlite database. There are only two buttons on the main activity - add information and read the contents of the database. It adds information well, there are no failures, but when I want to read information from the database in the second activity, it just crashes for me.
Here is the main page code (MainActivity)

package com.example.sqlite_students;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity  {
 
    public Button addBtn;
    public Button readBtn;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        addBtn = (Button) findViewById(R.id.addBtn);
        readBtn = (Button) findViewById(R.id.readBtn);
 
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddInformation.class);
                startActivity(intent);
 
            }
        });
        readBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ReadInformation.class);
                startActivity(intent);
            }
        });
    }
}

Here is my DB (DBHelper)
package com.example.sqlite_students;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class DBHelper extends SQLiteOpenHelper {
 
    public static final String DATABASE_NAME = "Students.db";
    public static final String TABLE_NAME = "Student_table";
 
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME_STUDENT";
    public static final String COL_3 = "SPECIAL_STUDENT";
    public static final String COL_4 = "COURSE_STUDENT";
 
    public DBHelper (Context context){
        super(context,DATABASE_NAME, null,1 );
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " +TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME_STUDENT TEXT, " +
                "SPECIAL_STUDENT TEXT,COURSE_STUDENT INTEGER ) ");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
    }
   // Добавление информации в БД
    public boolean insertData( String name_student, String special_student, String course_student){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name_student );
        contentValues.put(COL_3,special_student );
        contentValues.put(COL_4,course_student );
 
        long result = db.insert(TABLE_NAME, null, contentValues);
        db.close();
 
        if (result == -1){
            return false;
        } else return true;
    }
    // Вывод информации из БД
    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor result = db.rawQuery("Select * from " +TABLE_NAME, null);
        return result;
    }
}

And here is my problematic activity where information from the database (ReadInformation) should be displayed. First, I will show the markup, then the code itself.
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".ReadInformation">
 
   <ListView
       android:id="@+id/allInf"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
 
   </ListView>
 
</RelativeLayout>


package com.example.sqlite_students;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.DropBoxManager;
import android.util.Log;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import static com.example.sqlite_students.DBHelper.COL_2;
 
public class ReadInformation extends AppCompatActivity {
    private DBHelper myDB;
    public ListView informations;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_information);
 
        myDB = new DBHelper(this);
        informations = (ListView) findViewById(R.id.allInf);
 
        ReceivedData();
     }
 
    public void ReceivedData() {
        SQLiteDatabase db = myDB.getWritableDatabase();
        Cursor cursor = db.query("Student_table", new String[]{"ID","NAME_STUDENT","SPECIAL_STUDENT","COURSE_STUDENT"},
                null,null,null, null, null, null );
        if(cursor.moveToFirst()){
            String name = cursor.getString(1);
            String special = cursor.getString(2);
            String course = cursor.getString(3);
 
            CursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,
                    new String[] {"NAME_STUDENT"}, new int[] {android.R.id.text1},0 );
            informations.setAdapter(listAdapter);
        }
     
        db.close();
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-10-29
@zagayevskiy

It writes to you, well.

Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

The CursorAdapter requires a column named "_id". You can add such an ID or add something like SELECT ID AS _id to the query.
Better stop using ListView in general and CursorAdapter in particular. Go to RecyclerView.

D
DeNissss4444, 2020-10-30
@DeNissss4444

I already know how to work with RecyclerView =) I'm just learning android now and therefore I also need to be able to work with ListView, so I'm learning =) Thank you very much for your answer, I added "rowid _id" to new String and it all worked. Thank you very much)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question