首页 > 解决方案 > Android 应用程序强制关闭,SQLite 数据库的东西

问题描述

我正在开发我的项目应用程序,以了解有关 Android 应用程序开发过程的更多信息。我正在关注一个在线教程,但我似乎遇到了 SQLite 数据库的错误。它编译得很好,但是一旦运行就会强制关闭。这是 logcat 输出:

05-04 12:33:21.980 4403-4403/? I/art: Late-enabling -Xcheck:jni
05-04 12:33:22.177 4403-4403/com.example.a1713500104_crudsqlite_9 W/ActivityThread: Application com.example.a1713500104_crudsqlite_9 is waiting for the debugger on port 8100...
05-04 12:33:22.187 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: Sending WAIT chunk
05-04 12:33:22.594 4403-4410/com.example.a1713500104_crudsqlite_9 I/art: Debugger is active
05-04 12:33:22.792 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: Debugger has connected
05-04 12:33:22.792 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:23.014 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:23.216 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:23.417 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:23.617 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:23.817 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:24.018 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:24.218 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:24.418 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:24.619 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: waiting for debugger to settle...
05-04 12:33:24.820 4403-4403/com.example.a1713500104_crudsqlite_9 I/System.out: debugger has settled (1454)
05-04 12:33:25.079 4403-4403/com.example.a1713500104_crudsqlite_9 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-04 12:33:25.130 4403-4403/com.example.a1713500104_crudsqlite_9 I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
05-04 12:33:25.130 4403-4403/com.example.a1713500104_crudsqlite_9 I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
05-04 12:33:25.188 4403-4403/com.example.a1713500104_crudsqlite_9 W/ResourceType: Failure getting entry for 0x7f06005a (t=5 e=90) (error -75)
05-04 12:33:25.188 4403-4403/com.example.a1713500104_crudsqlite_9 D/AndroidRuntime: Shutting down VM
05-04 12:33:25.190 4403-4403/com.example.a1713500104_crudsqlite_9 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.a1713500104_crudsqlite_9, PID: 4403
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a1713500104_crudsqlite_9/com.example.a1713500104_crudsqlite_9.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
        at android.app.ActivityThread.access$800(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5305)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class Button
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.a1713500104_crudsqlite_9.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
        at android.app.ActivityThread.access$800(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5305) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
     Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.a1713500104_crudsqlite_9:drawable/icontambah" (7f06005a)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f06005a a=-1 r=0x7f06005a}
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2441)
        at android.content.res.Resources.loadDrawable(Resources.java:2381)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
        at android.widget.TextView.<init>(TextView.java:837)
        at android.widget.Button.<init>(Button.java:111)
        at android.widget.Button.<init>(Button.java:107)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:71)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:67)
        at androidx.appcompat.app.AppCompatViewInflater.createButton(AppCompatViewInflater.java:187)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:110)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.a1713500104_crudsqlite_9.MainActivity.onCreate(MainActivity.java:30) 
        at android.app.Activity.performCreate(Activity.java:5990) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
        at android.app.ActivityThread.access$800(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5305) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
05-04 12:33:39.356 4403-4403/com.example.a1713500104_crudsqlite_9 I/Process: Sending signal. PID: 4403 SIG: 9

这是我的代码:

数据库配置文件

package com.example.a1713500104_crudsqlite_9;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbConfig extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "biodatamahasiswa.db";
    private static final int DATABASE_VERSION = 1;
    public DbConfig (Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table biodata (no integer primary key,nama text null,tgl text null,jk text null,alamat text null)";
        Log.d("Data","OnCreate: "+sql);
        db.execSQL(sql);
        sql = "INSERT INTO biodata (no,nama,tgl,jk,alamat) VALUES ('1','Aulia Azmi','1998-07-14','Laki-Laki','Petamburan','Jakarta')";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    }
}

MainActivity.Java :

package com.example.a1713500104_crudsqlite_9;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity<item> extends AppCompatActivity {

    String[] daftar;
    ListView Listview01;
    Menu menu;
    protected Cursor cursor;
    DbConfig dbcenter;
    public static MainActivity ma;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.button2);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg1) {
                Intent inte = new Intent(MainActivity.this,BuatBiodataActivity.class);
                startActivity(inte);

            }
        });

        ma = this;
        dbcenter = new DbConfig(this);
        RefreshList();
    }

    public void RefreshList() {
        SQLiteDatabase db = dbcenter.getReadableDatabase();
        cursor = db.rawQuery("SELECT*FROM BIODATA",null);
        daftar = new String[cursor.getCount()];
        cursor.moveToFirst();
        for (int cc = 0;cc<cursor.getCount();cc++) {
            cursor.moveToPosition(cc);
            daftar[cc] = cursor.getString(1).toString();
        }
        Listview01 = (ListView) findViewById(R.id.listView1);
        Listview01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,daftar));
        Listview01.setSelected(true);
        Listview01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                final String selection = daftar[arg2];
                final CharSequence[] dialogitem = {"Lihat Biodata","Update Biodata","Hapus Biodata"};
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Pilihan");
                builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        switch (item) {
                            case 0:
                                Intent i = new Intent(getApplicationContext(),LihatBiodataActivity.class);
                                i.putExtra("nama",selection);
                                startActivity(i);
                                break;
                            case 1:
                                Intent in = new Intent(getApplicationContext(),UpdateBiodataActivity.class);
                                in.putExtra("nama",selection);
                                startActivity(in);
                                break;
                            case 2:
                                SQLiteDatabase db = dbcenter.getWritableDatabase();
                                db.execSQL("delete from biodata where nama='"+selection+"'");
                                RefreshList();
                                break;
                        }
                    }
                });
                builder.create().show();
                }

            });
        ((ArrayAdapter) Listview01.getAdapter()).notifyDataSetInvalidated();
        }
    }

BuatBiodataActivity.Java

package com.example.a1713500104_crudsqlite_9;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class BuatBiodataActivity extends AppCompatActivity {
    protected Cursor cursor;
    DbConfig dbHelper;
    Button ton1,ton2;
    EditText text1,text2,text3,text4,text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buat_biodata);

        dbHelper = new DbConfig(this);
        text1 = (EditText) findViewById(R.id.editText1);
        text2 = (EditText) findViewById(R.id.editText2);
        text3 = (EditText) findViewById(R.id.editText3);
        text4 = (EditText) findViewById(R.id.editText4);
        text5 = (EditText) findViewById(R.id.editText5);
        ton1 = (Button) findViewById(R.id.button1);
        ton2 = (Button) findViewById(R.id.button2);

        ton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.execSQL("insert into biodata(no,nama,tgl,jk,alamat)values('"+
                        text1.getText().toString()+"','"+
                        text2.getText().toString()+"','"+
                        text3.getText().toString()+"','"+
                        text4.getText().toString()+"','"+
                        text5.getText().toString()+"')");
                Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_SHORT).show();
                MainActivity.ma.RefreshList();
                finish();
            }
        });
        ton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }
}

LihatBiodataActivity.Java

package com.example.a1713500104_crudsqlite_9;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class LihatBiodataActivity extends AppCompatActivity {
    protected Cursor cursor;
    DbConfig dbHelper;
    Button ton2;
    TextView text1,text2,text3,text4,text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lihat_biodata);

        dbHelper = new DbConfig(this);
        text1 = (TextView) findViewById(R.id.textView1);
        text2 = (TextView) findViewById(R.id.textView2);
        text3 = (TextView) findViewById(R.id.textView3);
        text4 = (TextView) findViewById(R.id.textView4);
        text5 = (TextView) findViewById(R.id.textView5);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        cursor = db.rawQuery("SELECT*FROM biodata WHERE nama='"+getIntent().getStringExtra("nama")
                +"'",null);
        cursor.moveToFirst();
        if (cursor.getCount()>0) {
            cursor.moveToPosition(0);
            text1.setText(cursor.getString(0).toString());
            text2.setText(cursor.getString(1).toString());
            text3.setText(cursor.getString(2).toString());
            text4.setText(cursor.getString(3).toString());
            text5.setText(cursor.getString(4).toString());
        }
        ton2 = (Button) findViewById(R.id.button1);
        ton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }
}

更新BiodataActivity.Java

package com.example.a1713500104_crudsqlite_9;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class UpdateBiodataActivity extends AppCompatActivity {
    protected Cursor cursor;
    DbConfig dbHelper;
    Button ton1, ton2;
    EditText text1, text2, text3, text4, text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_biodata);

        dbHelper = new DbConfig(this);
        text1 = (EditText) findViewById(R.id.editText1);
        text2 = (EditText) findViewById(R.id.editText2);
        text3 = (EditText) findViewById(R.id.editText3);
        text4 = (EditText) findViewById(R.id.editText4);
        text5 = (EditText) findViewById(R.id.editText5);
        final SQLiteDatabase db = dbHelper.getReadableDatabase();

        cursor = db.rawQuery("SELECT*FROM biodata WHERE nama='" + getIntent().getStringExtra("nama") +
                "'", null);
        cursor.moveToFirst();
        if (cursor.getCount()>0){
            cursor.moveToPosition(0);
            text1.setText(cursor.getString(0).toString());
            text2.setText(cursor.getString(1).toString());
            text3.setText(cursor.getString(2).toString());
            text4.setText(cursor.getString(3).toString());
            text5.setText(cursor.getString(4).toString());
        }
        ton1 = (Button) findViewById(R.id.button1);
        ton2 = (Button) findViewById(R.id.button2);

        ton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                db.execSQL("update biodata set nama='" +
                        text2.getText().toString() + "',tgl='" +
                        text3.getText().toString() + "',jk='" +
                        text4.getText().toString() + "',alamat='" +
                        text5.getText().toString() + "'where no='" +
                        text1.getText().toString() + "'");
                Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
                MainActivity.ma.RefreshList();
                finish();
            }
        });
        ton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

    }
}

如果有人可以帮助我找到代码中的错误并提出修复建议,我将不胜感激!非常感谢您帮助像我这样的菜鸟:)

标签: javascriptandroidsqlsqlite

解决方案


您应该将您的按钮定义为MainActivity.Java.

String[] daftar;
ListView Listview01;
Menu menu;
protected Cursor cursor;
DbConfig dbcenter;
public static MainActivity ma;
Button btn; // <------

并在方法onCreate中,为其分配一个对象。

btn = (Button) findViewById(R.id.button2);

推荐阅读