A
A
aeaeae12021-05-03 02:03:47
Java
aeaeae1, 2021-05-03 02:03:47

Java application crashes - android studio, what is the reason?

Hello! I have an application in Android studio. When connecting to the database, the application crashes, I can’t understand
the reason. Here is the code.
DatabaseAccess class

package com.example.myapplication;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class DatabaseAccess {
 
    private SQLiteOpenHelper openHelper;
    private SQLiteDatabase db;
    private static DatabaseAccess instance;
    Cursor c=null;
 
    private DatabaseAccess(Context context){
        this.openHelper=new DatabaseOpenHelper(context);
 
 
    }
 
    public static DatabaseAccess getInstance(Context context)
    {
        if(instance==null)
        {
            instance=new DatabaseAccess(context);
 
        }
        return instance;
    }
 
 
    public void open()
    {
        this.db=openHelper.getWritableDatabase();
 
    }
 
    public void close() {
        if (db != null) {
            this.db.close();
        }
    }
 
    public String getAddress(String name)
    {
        c=db.rawQuery("select Address from Table1 where Name='"+name+"'",new String[]{});
        StringBuffer buffer =new StringBuffer();
        while(c.moveToNext())
        {
            String address  = c.getString(0);
            buffer.append(""+address);
 
        }
        return buffer.toString();
    }
 
}

main activity class
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
    public EditText name;
    public Button guery_button;
    public TextView result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        name=findViewById(R.id.name);
        guery_button=findViewById(R.id.guery_button);
        result=findViewById(R.id.result);
 
        guery_button.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View view) {
 
                DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
                databaseAccess.open();
 
                String n=name.getText().toString();
                String address=databaseAccess.getAddress(n);
 
                result.setText(address);
                databaseAccess.close();
            }
 
        });
 
 
 
    }
}
    }
}

DatabaseOpenHelper
package com.example.myapplication;
import android.content.Context;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
 
public class DatabaseOpenHelper extends SQLiteAssetHelper {
    private static final String DATABASE_NAME="Database.db";
    private static final int DATABASE_VERSION=1;
 
    public  DatabaseOpenHelper(Context context)
    {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
 
 
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question