2013-04-11 213 views
0

你好我正在編寫一個用戶能夠查看的數據庫,其中包含有關我用來填充數據庫的數據的信息。在手機或模擬器上運行活動時,應用程序崩潰。沒有logcat問題,所以我不確定可能是什麼問題。如果任何人都可以提供給我任何提示,我將不勝感激,因爲我是新手,在Java編程和使用Android ADT。以下是我的活動代碼。我使用當前版本爲Build:Eclipse ADT Android sqlite:應用程序在打開後立即崩潰

package com.example.database; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class MainActivity 
{ 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_BUSINESS = "business"; 
    public static final String KEY_ADDRESS = "address"; 
    public static final String KEY_PHONE = "phone"; 
    public static final String KEY_HOURS = "hours"; 
    public static final String KEY_WEB = "website"; 
    public static final String KEY_TYPE = "type"; 

    private static final String TAG = "MainActivity"; 

    private static final String DATABASE_NAME = "BloomBusiness"; 
    private static final String DATABASE_TABLE = "Business"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table Business (_id integer primary key autoincrement, " 
     +"business text not null, address text not null, hours text not null"+"web text not null"+"type text not null" 
     + "phone text not null);"; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public MainActivity(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS Business"); 
      onCreate(db); 
     } 
    }  

    //---opens the database--- 
    public MainActivity open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---closes the database---  
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //---insert a title into the database--- 
    public long insertTitle(String business, String address, String phone, String hours, String website, String type) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_BUSINESS, business); 
     initialValues.put(KEY_ADDRESS, address); 
     initialValues.put(KEY_PHONE, phone); 
     initialValues.put(KEY_HOURS, hours); 
     initialValues.put(KEY_WEB, website); 
     initialValues.put(KEY_TYPE, type); 

     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //---deletes a particular title--- 
    public boolean deleteTitle(long rowId) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + 
       "=" + rowId, null) > 0; 
    } 

    //---retrieves all the titles--- 
    public Cursor getAllTitles() 
    { 
     return db.query(DATABASE_TABLE, new String[] { 
       KEY_ROWID, 
       KEY_BUSINESS, 
       KEY_ADDRESS, 
       KEY_PHONE, 
       KEY_HOURS, 
       KEY_WEB, 
       KEY_TYPE}, 
       null, 
       null, 
       null, 
       null, 
       null); 
    } 

    //---retrieves a particular title--- 
    public Cursor getTitle(long rowId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(DATABASE_TABLE, new String[] { 
         KEY_ROWID, 
         KEY_BUSINESS, 
         KEY_ADDRESS, 
         KEY_PHONE, 
         KEY_HOURS, 
         KEY_WEB, 
         KEY_TYPE}, 
         KEY_ROWID + "=" + rowId, 
         null, 
         null, 
         null, 
         null, 
         null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    //---updates a title--- 
    public boolean updateTitle(long rowId, String business, 
    String address, String phone, String hours, String website,String type) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_BUSINESS, business); 
     args.put(KEY_ADDRESS, address); 
     args.put(KEY_PHONE, phone); 
     args.put(KEY_HOURS,hours); 
     args.put(KEY_WEB, website); 
     args.put(KEY_TYPE, type); 
     return db.update(DATABASE_TABLE, args, 
         KEY_ROWID + "=" + rowId, null) > 0; 
    } 
} 

返回所有的企業名稱的列表:

package com.example.database; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.Toast; 

public class DBUse extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     MainActivity db = new MainActivity(this); 


     db.open(); 
     Cursor c = db.getAllTitles(); 
     if(c.moveToFirst()) 
     { 
      do{DisplayTitle(c); 
     }while (c.moveToNext()); 
    } 
     db.open(); 
     Cursor b = db.getTitle(1); 

     if (b.moveToFirst()) 
      DisplayTitle(c); 
     else 
      Toast.makeText(this,"No business found",Toast.LENGTH_LONG).show(); 
     db.close(); 
    db.close(); 


} 

    public void DisplayTitle(Cursor c) { 
     Toast.makeText(this, 
     "Name: " + c.getString(1)+"\n"+ 
     "Address:" + c.getString(2)+"\n"+ 
     "Phone:" + c.getString(3)+"\n"+ 
     "Hours:" + c.getString(4)+"\n", 
     Toast.LENGTH_LONG).show(); 
    } 


    } 
v21.1.0-569685

主要在數據庫中創建活動

輸入數據到數據庫:

package com.example.database; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.Toast; 

public class DBUse extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     MainActivity db = new MainActivity(this); 


     db.open(); 
     Cursor c = db.getAllTitles(); 
     if(c.moveToFirst()) 
     { 
      do{DisplayTitle(c); 
     }while (c.moveToNext()); 
    } 
     db.open(); 
     Cursor b = db.getTitle(1); 

     if (b.moveToFirst()) 
      DisplayTitle(c); 
     else 
      Toast.makeText(this,"No business found",Toast.LENGTH_LONG).show(); 
     db.close(); 
    db.close(); 


} 

    public void DisplayTitle(Cursor c) { 
     Toast.makeText(this, 
     "Name: " + c.getString(1)+"\n"+ 
     "Address:" + c.getString(2)+"\n"+ 
     "Phone:" + c.getString(3)+"\n"+ 
     "Hours:" + c.getString(4)+"\n", 
     Toast.LENGTH_LONG).show(); 
    } 


    } 

最後清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.database" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     android:debuggable="true"> 
     <activity 
      android:name="com.example.database.DBUse" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

更改清單後,我收到了我的控制檯中的這個錯誤,但沒有顯示在logcat中。

[2013-04-10 23:46:43 - database] Performing com.example.database.MainActivity activity launch 
[2013-04-10 23:46:47 - database] Application already deployed. No need to reinstall. 
[2013-04-10 23:46:47 - database] Starting activity com.example.database.MainActivity on device 0A3BB5F106008016 
[2013-04-10 23:46:48 - database] New package not yet registered with the system. Waiting 3 seconds before next attempt. 
[2013-04-10 23:46:51 - database] Starting activity com.example.database.MainActivity on device 0A3BB5F106008016 
[2013-04-10 23:46:51 - database] New package not yet registered with the system. Waiting 3 seconds before next attempt. 
[2013-04-10 23:46:54 - database] Starting activity com.example.database.MainActivity on device 0A3BB5F106008016 
[2013-04-10 23:46:54 - database] New package not yet registered with the system. Waiting 3 seconds before next attempt. 
[2013-04-10 23:46:57 - database] Starting activity com.example.database.MainActivity on device 0A3BB5F106008016 
[2013-04-10 23:46:58 - database] New package not yet registered with the system. Waiting 3 seconds before next attempt. 
[2013-04-10 23:47:01 - database] Starting activity com.example.database.MainActivity on device 0A3BB5F106008016 
[2013-04-10 23:47:01 - database] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.database/.MainActivity } 
[2013-04-10 23:47:01 - database] ActivityManager: Error type 3 
[2013-04-10 23:47:01 - database] ActivityManager: Error: Activity class {com.example.database/com.example.database.MainActivity} does not exist. 

回答

0

應該改變你的清單以

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:debuggable="true"> 
    <activity 
     android:name="com.example.database.DBUse" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

MainActivity不延伸活動,應該說是將對DBAdapter。

+0

我剛剛嘗試了您的建議,現在當我嘗試在手機上運行應用程序時出現此錯誤。 [2013-04-10 23:47:01 - 數據庫] ActivityManager:啓動:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.database /.MainActivity} [2013-04-10 23:47:01 - 數據庫] ActivityManager:錯誤類型3 [2013-04-10 23:47:01 - 數據庫] ActivityManager:錯誤:Activity類{com.example。 database/com.example.database.MainActivity}不存在。 – aNappyBoi 2013-04-11 03:48:00

+0

你收到了什麼錯誤?發佈logcat。 – 2013-04-11 03:49:44

+0

您是否完全按照我在上面編輯我的回答的方式來使用它? – 2013-04-11 03:53:33

相關問題