2016-01-03 56 views
0

我想創建一個數據庫實例。這就是爲什麼我有以下OCDE:方法openOrCreateDatabase(String,int,null)得到異常「無法打開數據庫」

public class ContactFragment extends SherlockFragment { 

    private static final String THIS_FILE = "ContactFragment"; 
    ImageButton new_contact ; 
    ListView listView; 
    ArrayAdapter arrayAdapter; 
    SQLiteDatabase database; 
    List<CONTACT_ITEM> contactBody; 
    int MODE_PRIVATE=1; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     database = openOrCreateDatabase("ContactDatabase",null); 


    // database.execSQL("CREATE TABLE if not exists constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, number text , extension text);"); 

    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.contactdialer, container, false); 

     contactBody = getReadFromDatabase(); 
     new_contact = (ImageButton) rootView.findViewById(R.id.imageButton); 
     new_contact.setOnClickListener(this); 

     listView = (ListView) rootView.findViewById(R.id.list_view); 
     arrayAdapter=new ContactAdapter(getActivity(),R.layout.row_item_contact,contactBody); 
     listView.setAdapter(arrayAdapter); 

     return rootView; 
    } 

    List<CONTACT_ITEM> getReadFromDatabase() 
    { 
      List<CONTACT_ITEM> contact = new ArrayList<CONTACT_ITEM>(); 
      return contact; 
    } 

} 

但我在follwing線變得異常:

database = openOrCreateDatabase("ContactDatabase",null); 

的logcat中給出了以下異常:

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 
      at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 
      at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 
      at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 
      at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 
      at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 
      at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 
      at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:829) 
      at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:814) 
      at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:709) 
      at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:724) 
      at com.csipsimple.ui.contacts.ContactFragment.onCreate(ContactFragment.java:38) 
      at android.support.v4.app.Fragment.performCreate(Fragment.java:1763) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) 
      at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486) 
      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1073) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:919) 
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) 
      at android.view.View.measure(View.java:16848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:340) 
      at android.view.View.measure(View.java:16848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374) 
      at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 
      at android.view.View.measure(View.java:16848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:340) 
      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2368) 
      at android.view.View.measure(View.java:16848) 
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2246) 
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1312) 
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509) 
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189) 
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
      at android.view.Choreographer.doFrame(Choreographer.java:560) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774) 
      at android.os.Handler.handleCallback(Handler.java:808) 
      at android.os.Handler.dispatchMessage(Handler.java:103) 
      at android.os.Looper.loop(Looper.java:193) 
      at android.app.ActivityThread.main(ActivityThread.java:5330) 
      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:828) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
      at dalvik.system.NativeStart.main(Native Method) 

我爲什麼得到這個錯誤?我該如何解決這個問題?任何建議都很有幫助。

回答

0

公共抽象SQLiteDatabase openOrCreateDatabase(字符串名稱,詮釋方式,SQLiteDatabase.CursorFactory廠)

打開與此上下文的應用程序包關聯的新私人SQLiteDatabase。如果數據庫文件不存在,請創建它。

參數 name數據庫的名稱(在應用程序包中是唯一的)。 模式運行模式。默認操作MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE使用0或MODE_PRIVATE來控制權限。使用MODE_ENABLE_WRITE_AHEAD_LOGGING默認啓用預寫日誌記錄。 factory在調用查詢時被調用來實例化遊標的可選工廠類。 返回

拋出 SQLiteException如果數據庫文件無法打開。

openOrCreateDatabase(字符串,整數,SQLiteDatabase.CursorFactory) 使用

database = openOrCreateDatabase("ContactDatabase",0); 
    database = openOrCreateDatabase("ContactDatabase",MODE_PRIVATE); 
+1

如果我使用數據庫= openOrCreateDatabase( 「ContactDatabase」,MODE_PRIVATE);然後編譯器顯示無法解析方法openOrCreateDatabase(String,int): –