我通過Content Provider創建數據庫應用程序,但出現錯誤,我不知道如何解決此問題,但我仍嘗試過,但仍然存在一個錯誤。請幫她從專家:)謝謝無法啓動活動ComponentInfo,數據庫內容提供程序「我的應用程序強制關閉」
12-29 18:57:00.333 31234-31234/com.example.arf_amella.rencana E/AndroidRuntime:致命異常:主要 流程:com.example.arf_amella .rencana,PID:31234 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.arf_amella.rencana/com.example.arf_amella.rencana.MainActivity}:android.database.sqlite.SQLiteException:no such table: rencana(代碼1):,編譯時:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)上刪除rencana at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) at android.app.ActivityThread.access $ 800(ActivityThread.java:166) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102 ) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteIn it.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) 由android.database.sqlite.SQLiteException引起:no such table:rencana(code 1):,while compiling:DELETE FROM android.database.sqlite.SQLiteConnection.nativePrepareStatement(本地方法)從rencana 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1112) android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java (SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1613) at com.example.arf_amella.rencana.RencanaKerja.delete(RencanaKerja。 java:109) at android.content.ContentProvider $ Transport.delete(ContentProvider.java:273) at android.content.ContentResolver.delete(ContentResolver.java:1282) at com.example.arf_amella.rencana.MainActivity。 onCreate(MainActivity.java:59) at android.app.Activity.performCreate(Activity.java:5442) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)在android.app.ActivityThread.access $ 800(ActivityThread.java:166) (android.app.ActivityThread.java:2493) at android.app.ActivityThread $ H.HandleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper。的java:136) 在android.app.ActivityThread.main(ActivityThread.java:5584) 在java.lang.reflect.Method.invokeNative(本機方法) 在java.lang.reflect.Method.invoke(Method.java :515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system .NativeStart.main(本機方法)
DBhelper.java
public class DBHelper extends SQLiteOpenHelper {
//Berisi perintah SQL untuk menciptakan Tabel bernama artis
private final static String BUAT_TABEL = "create table" +
Konstanta.NAMA_TABEL + " (" +
Konstanta.ID_RENCANA + " integer primary key autoincrement, " +
Konstanta.SIFAT_RENCANA + " text not null, " +
Konstanta.NAMA_RENCANA + " text not null);";
public DBHelper(Context konteks) {
super(konteks, Konstanta.NAMA_DB, null, Konstanta.VERSI_DB);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(BUAT_TABEL);
}
catch (SQLiteException e) {
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int versilama, int versibaru) {
db.execSQL("drop table if exists " + Konstanta.NAMA_TABEL);
onCreate(db);
}
}
Konstanta.java
public class Konstanta {
public final static String NAMA_DB = "dbrencana";
public final static int VERSI_DB = 1;
public final static String NAMA_TABEL = "rencana";
public final static String ID_RENCANA = "id";
public final static String NAMA_RENCANA = "nama";
public final static String SIFAT_RENCANA = "sifat";
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitial;
EditText editTextRencana = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
//Mendeklarasikan Interstitial
mAdView.loadAd(adRequest);
interstitial = new InterstitialAd(MainActivity.this);
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
displayInterstitial();
}
});
//Hapus Semua Record jika ada
getContentResolver().delete(RencanaKerja.URI_ISI,
null,
null);
//Tambahkan data
simpan("Main Tenis", "Biasa");
simpan("Rapat Hari Rabu", "Penting");
simpan("Tugas Pemrograman Web", "Penting");
simpan("Main Futsal", "Biasa");
//Tampilkan Data
ArrayList<String> listData = perolehanData();
final ListView listViewData = (ListView) findViewById(R.id.listViewRencana);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listData);
listViewData.setAdapter(adapter);
}
public void simpan(String rencana, String sifat) {
ContentValues data = new ContentValues();
data.put(RencanaKerja.NAMA_RENCANA, rencana);
data.put(RencanaKerja.SIFAT_RENCANA, sifat);
getContentResolver().insert(RencanaKerja.URI_ISI, data);
}
private ArrayList<String> perolehanData() {
ArrayList<String> hasil = new ArrayList<String>();
Uri semuaRencana = RencanaKerja.URI_ISI;
Cursor c = getContentResolver().query(
semuaRencana,
null, null, null, null);
if (c.moveToFirst()){
do {
String namaRencana = c.getString(c.getColumnIndex("nama"));
String sifatRencana = c.getString(c.getColumnIndex("sifat"));
hasil.add(namaRencana + " (" + sifatRencana + ") ");
} while (c.moveToNext());
}
return hasil;
}
private void displayInterstitial() {
//Meneampilkan Interestitial
if (interstitial.isLoaded()){
interstitial.show();
}
}
}
RencanaKerja.java
public class RencanaKerja extends ContentProvider {
public static final String NAMA_PROVIDER = "com.example.arf_amella.rencana";
public static Uri URI_ISI = Uri.parse("content://" +
NAMA_PROVIDER + "/rencana");
public static final String _ID = Konstanta.ID_RENCANA;
public static final String NAMA_RENCANA = Konstanta.NAMA_RENCANA;
public static final String SIFAT_RENCANA = Konstanta.SIFAT_RENCANA;
private static final int RENCANA = 1;
private static final int ID_RENCANA = 2;
private DBHelper dbHelper;
private SQLiteDatabase dbRencana;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
uriMatcher.addURI(NAMA_PROVIDER, "rencana", RENCANA);
uriMatcher.addURI(NAMA_PROVIDER, "rencana/#", ID_RENCANA);
}
//Dipanggil Saat Content Provider Dipanggil
@Override
public boolean onCreate() {
dbHelper = new DBHelper(getContext());
dbRencana = dbHelper.getWritableDatabase();
return (dbRencana == null) ? false : true;
}
//Memberi tipe MIME data untuk URI yg diberikan
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
//Menambahkan Record baru ke content Provider
@Nullable
@Override
public Uri insert(Uri uri, ContentValues cv) {
long IDBaris = dbRencana.insert(Konstanta.NAMA_TABEL, null, cv);
if (IDBaris > 0){
Uri sUri = ContentUris.withAppendedId(URI_ISI, IDBaris);
getContext().getContentResolver().notifyChange(sUri, null);
return sUri;
}
throw new SQLException("Gagal Menyisipkan Data Ke "+ uri);
}
// Mengubah Data disuatu Record Content Provider
@Override
public int update(Uri uri, ContentValues cv, String seleksi, String[] argSeleksi) {
int tipeUri = uriMatcher.match(uri);
int cacah = 0;
switch (tipeUri){
case RENCANA:
cacah = dbRencana.update(Konstanta.NAMA_TABEL,cv,seleksi,argSeleksi);
break;
case ID_RENCANA:
cacah = dbRencana.update(Konstanta.NAMA_TABEL,cv,_ID+" = "+uri.getPathSegments().get(1),null);
break;
default:
throw new IllegalArgumentException("URI " + uri + " tidak dikenal");
}
getContext().getContentResolver().notifyChange(uri, null);
return cacah;
}
//menghapus Record pada Content PROVIDER
@Override
public int delete(Uri uri, String seleksi, String[] argSeleksi) {
int tipeUri = uriMatcher.match(uri);
int cacah = 0;
switch (tipeUri) {
case RENCANA:
cacah = dbRencana.delete(Konstanta.NAMA_TABEL, seleksi, argSeleksi);
break;
case ID_RENCANA :
cacah = dbRencana.delete(Konstanta.NAMA_TABEL, _ID + " = "+ uri.getPathSegments().get(1), null);
break;
default:
throw new IllegalArgumentException("URI" + uri + " tidak dikenal ");
}
getContext().getContentResolver().notifyChange(uri, null);
return cacah;
}
// Memproses Query
@Nullable
@Override
public Cursor query(Uri uri, String[] proyeksi, String seleksi, String[] argSeleksi, String pengurutan) {
SQLiteQueryBuilder sqLBuilder = new SQLiteQueryBuilder();
sqLBuilder.setTables(Konstanta.NAMA_TABEL);
if (uriMatcher.match(uri) == ID_RENCANA)
sqLBuilder.appendWhere(Konstanta.ID_RENCANA + " = " + uri.getPathSegments().get(1));
if (pengurutan == null || pengurutan == "")
pengurutan = Konstanta.NAMA_RENCANA;
Cursor c = sqLBuilder.query(dbRencana, proyeksi, seleksi, argSeleksi, null, null, pengurutan);
getContext().getContentResolver().notifyChange(uri, null);
return c;
}
}
AndroidManifest
<provider
android:name="RencanaKerja"
android:authorities="com.example.arf_amella.rencana"
android:exported="true"
/>
「創建表」 - >「創建表」 – pskink
對不起,我不明白.. @pskink –