0

我在将数据库文件从 /assets 复制到文件资源管理器中的 /data/data 文件夹时遇到问题。我搜索了这个网站,找到了很多答案,但找不到适合我上下文的合适答案。我已经使用 SQLite Manager 在外部创建了数据库并将其导入到 assets 文件夹中。现在,当我运行我的应用程序时,我在模拟器中得到了 NullPointerException。我发现包根本没有在 /data/data 文件夹中创建。但是该应用程序正在模拟器中启动。调试器也没有显示任何错误。

我尝试了以下解决方案 -

重新启动eclipse和模拟器,删除并重新创建现有模拟器,最后重新启动笔记本电脑

没有一个解决方案解决了我的问题。谁能告诉我,我的错误是什么?

这是我的 MainActivity.java :-

public class MainActivity extends Activity implements OnClickListener
{
Dialog d;
private photoDbAdapters mDbAdapter;
public int currentimageindex=0;
 String[] sp;   

int p=1;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        images();
 }
 public void images()
 {
        try{
            String rt=mDbAdapter.fetchsingles(); //the application 
                                           is getting crashed in this part

        int id = getResources().getIdentifier(rt, null, null);
        ImageView iv = (ImageView) findViewById(R.id.ImageView3_Left);
        iv.setImageResource(id);
    }catch(NullPointerException er)
{
        String ht=er.toString();
        Toast.makeText(getApplicationContext(), ht, Toast.LENGTH_LONG).show    ();
    }}


@Override
public void onClick(View v)
{
    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
    // TODO Auto-generated method stub
}

fetchsingles 方法:-(此方法将从数据库中检索图像文件名)

public String fetchsingles()
{

    try{

img = mDbHelper.getData();


    }catch(Exception e)
    {
        String error= e.toString();
        Dialog d = new Dialog(null);
        d.setTitle("image cannot be fetched");
        int err=Integer.parseInt(error);
        d.setContentView(err);
        d.show();
    }
    return img;


}

获取数据方法:-

public String getData() 
{
    // TODO Auto-generated method stub

    String dry="SELECT "+COL_DATA+" FROM Photos WHERE "+COL_ID+"=2;";

        Cursor c = myDataBase.rawQuery(dry,null);

        String result = "";
        int img = c.getColumnIndex(COL_DATA);


            result = c.getString(img);





    return dry;

代码可能看起来很长,不要介意任何事情,请帮助我解决这个问题。

提前致谢。

4

1 回答 1

1

尝试使用以下代码将数据库从资产复制到 data/data/package 目录

package com.example.myapp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

      public class DataBaseHelper1 extends SQLiteOpenHelper{
      private static String DB_PATH = "/data/data/com.example.myapp/databases/";

      private static String DB_NAME = "myDB.sqlite";

      private SQLiteDatabase myDataBase;

      private final Context myContext;

      public DataBaseHelper1(Context context) 
      {
          super(context, DB_NAME, null, 1);
          this.myContext = context;
      }

      public void createDataBase() throws IOException{


    boolean dbExist = checkDataBase();

      if(dbExist)
      {
          Log.i("DB....", "database available....");
      }
      else
      {
          this.getWritableDatabase();

          try {

          copyDataBase();

          } catch (IOException e) {

          throw new Error("Error copying database");

          }

         Log.i("DB..", "database created.....");
       }   

      }


      public boolean checkDataBase(){

      SQLiteDatabase checkDB = null;

      try{

      String myPath = DB_PATH + DB_NAME;

      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

      }catch(SQLiteException e){

          Log.e("CheckDb","DB not found");
      //database does't exist yet.

      if(checkDB != null){

      checkDB.close();

      }
      }
      finally
      {
          if(checkDB != null){

              checkDB.close();

              } 
          this.close();
      }
      return checkDB != null ? true : false;

      }




      private void copyDataBase() throws IOException{

      InputStream myInput = myContext.getAssets().open(DB_NAME);

      String outFileName = DB_PATH + DB_NAME;

      OutputStream myOutput = new FileOutputStream(outFileName);
          byte[] buffer = new byte[1024];

      int length;

      while ((length = myInput.read(buffer))>0){

      myOutput.write(buffer, 0, length);

      }

      myOutput.flush();

      myOutput.close();

      myInput.close();

      }

      public SQLiteDatabase openDataBase() throws SQLException{

      String myPath = DB_PATH + DB_NAME;

      return myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

      }


      @Override

      public synchronized void close() {

      if(myDataBase != null)

      myDataBase.close();

      super.close();

      }

      @Override

      public void onCreate(SQLiteDatabase db) {

      }

      @Override

      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


      }  


      public void getData()
      {
          SQLiteDatabase myDB ;
          Cursor cursor ;
           try {

                myDB=this.openDataBase();                   

                    cursor=myDB.rawQuery("SELECT * FROM Country_Master",null);


                    if (cursor != null ) {
                       if  (cursor.moveToFirst()) {
                       do {

                           // put your code to get data from cursor                      

                       }while (cursor.moveToNext());
                       }

                    }



                   if(cursor != null)
                    {
                        myDB.close();
                       cursor.close();
                    }                      
                    }catch(SQLException sqle){

                    throw sqle;

                    }


        }
}

还要将此代码放在要复制数据库的活动中的 onCreate 方法下。

 DataBaseHelper1 myDbHelper = new DataBaseHelper1(MyActivity.this); 
     try 
     {
         myDbHelper.createDataBase();
     }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

     finally
     {
         myDbHelper.close();
     }
于 2013-08-29T08:55:56.640 回答