在电话,但不是在模拟器应用程序错误 [英] Application error on phone but not on emulator

查看:173
本文介绍了在电话,但不是在模拟器应用程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在Android的编程初学者,我没有什么问题。
我写了简单的数据库应用程序(我有现成的数据库),我需要将其复制到应用程序文件夹手机。我对这种方法。当我运行模拟器的应用程序,所有的作品,但是当我这个程序复制到我的手机,应用程序有崩溃。你能帮助我吗?
这是dbAdapter:

 进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口android.app.Application;
进口android.content.ContentValues​​;
进口android.content.Context;
进口android.content.ContextWrapper;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteDatabase.CursorFactory;
进口android.database.sqlite.SQLiteOpenHelper;公共类dbAdapter {
私有静态最后弦乐DB_NAME =names2.db;
私有静态最终诠释DB_VERSION = 1;
私有静态最后弦乐DB_TABLE =名;
串DB_PATH;
公共静态最后弦乐KEY_ID =_id;
公共静态最后弦乐KEY_NAME =名;
公共静态最终诠释NAME_COLUMN = 1;
公共静态最后弦乐KEY_SEX =性;
公共静态最终诠释SEX_COLUMN = 2;
公共静态最后弦乐KEY_RACE =种族;
公共静态最终诠释RACE_COLUMN = 3;
私人SQLiteDatabase分贝;
私人最终上下文的背景下;
私人DatabaseHelper myDatabaseHelper;公共dbAdapter(上下文_context){
    上下文= _context;
    // DB_PATH = context.getApplicationContext()getFilesDir()的getPath()。    DB_PATH = context.getFilesDir()的getPath()+/数据库/。
    myDatabaseHelper =新DatabaseHelper(_context,DB_NAME,空,DB_VERSION);
}公共dbAdapter的open(){
    DB = myDatabaseHelper.getReadableDatabase();
    返回此;
}公共无效的close(){
    db.close();
}众长插入(REKORD _rekord){
    ContentValues​​ newRekord =新ContentValues​​();
    newRekord.put(KEY_NAME,_rekord.getName());
    newRekord.put(KEY_SEX,_rekord.getSex());
    newRekord.put(KEY_RACE,_rekord.getRace());
    返回db.insert(DB_TABLE,空,newRekord);
}
公共字符串的getData(字符串性别,种族字符串){
    光标mycursor = db.rawQuery(选择的名字从名字WHERE性别=+ +性和种族=+赛跑+ORDER BY RANDOM()LIMIT 1,NULL);
      如果(mycursor.moveToFirst()){//根据从SAM建议编辑
          串strCatName = mycursor.getString(mycursor.getColumnIndex(KEY_NAME));
                  返回strCatName;
        }其他{
        返回null;
         }
      //返回db.rawQuery(选择的名字从名字WHERE性别= 1 ORDER BY RANDOM()LIMIT 1,新的String [] {名});
    }
公共无效copyDatabase()抛出IOException异常{// niby kopiowanie bazy danych。聂wiem CZY多布雷博聂chcedziałać    //打开本地数据库的输入流
    InputStream的myInput = context.getAssets()开(DB_NAME)。    //路径刚刚创建的空分贝
    字符串outFileName = DB_PATH + DB_NAME;    //打开空分贝的输出流
    的OutputStream myOutput =新的FileOutputStream(outFileName);    //传递从inputfile中字节到OUTPUTFILE
    字节[]缓冲区=新的字节[1024];
    INT长;
    而((长度= myInput.read(缓冲液))大于0){
        myOutput.write(缓冲液,0,长度);
    }    //关闭流
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
公共无效删除(){
    档案文件=新的文件(DB_PATH + DB_NAME);
    如果(file.exists())
        file.delete();
}公共SQLiteDatabase assetOpenDatabase(){
    文件DBFILE = context.getDatabasePath(DB_NAME);
    如果(!dbFile.exists()){
        尝试{
            assetCop​​yDatabase(DBFILE);
        }赶上(IOException异常五){
            抛出新的RuntimeException(错误创建源数据库,E);
        }
    }    返回SQLiteDatabase.openDatabase(dbFile.getPath(),空,SQLiteDatabase.OPEN_READONLY);
}私人无效assetCop​​yDatabase(文件DBFILE)抛出IOException
    InputStream为= context.getAssets()开(DB_NAME)。
    OutputStream的OS =新的FileOutputStream(DBFILE);    字节[]缓冲区=新的字节[1024];
    而(is.read(缓冲液)大于0){
        os.write(缓冲液);
    }    os.flush();
    os.close();
    is.close();
}
// ------------------------------------------------ --------
类DatabaseHelper扩展SQLiteOpenHelper {
    公共DatabaseHelper(上下文的背景下,字符串名称,CursorFactory工厂,诠释的版本){
        超(背景下,名称,厂家,版本);
    }
    @覆盖
    公共无效的onCreate(SQLiteDatabase _db){
        //_db.execSQL(\"CREATE表名(_id INTEGER PRIMARY KEY AUTOINCREMENT,名称文本NOT NULL,性别NUMERIC NOT NULL,种族NUMERIC NOT NULL));
    }
    @覆盖
    公共无效onUpgrade(SQLiteDatabase _db,诠​​释oldVer,诠释newVer){
        _db.execSQL(DROP TABLE IF EXISTS+ DB_TABLE);
        的onCreate(_db);
    }
}
}

这就是我如何复制文件:

  dbAdapter巴扎=新dbAdapter(本);
baza.copyDatabase();


    baza.assetOpenDatabase();
同样的效果。


  EHH,现在我对模拟器错误太...
06-11 21:31:20.510:E / AndroidRuntime(537):致命异常:主要
06-11 21:31:20.510:E / AndroidRuntime(537):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.namesgenerator / com.example.namesgenerator.sex}了java.lang.RuntimeException:错误创建源数据库
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.os.Handler.dispatchMessage(Handler.java:99)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.os.Looper.loop(Looper.java:123)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.ActivityThread.main(ActivityThread.java:3683)
06-11 21:31:20.510:E / AndroidRuntime(537):在java.lang.reflect.Method.invokeNative(本机方法)
06-11 21:31:20.510:E / AndroidRuntime(537):在java.lang.reflect.Method.invoke(Method.java:507)
06-11 21:31:20.510:E / AndroidRuntime(537):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 21:31:20.510:E / AndroidRuntime(537):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 21:31:20.510:E / AndroidRuntime(537):在dalvik.system.NativeStart.main(本机方法)
06-11 21:31:20.510:E / AndroidRuntime(537):了java.lang.RuntimeException:由造成错误创建源数据库
06-11 21:31:20.510:E / AndroidRuntime(537):在com.example.namesgenerator.dbAdapter.assetOpenDatabase(dbAdapter.java:108)
06-11 21:31:20.510:E / AndroidRuntime(537):在com.example.namesgenerator.sex.onCreate(sex.java:39)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-11 21:31:20.510:E / AndroidRuntime(537):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-11 21:31:20.510:E / AndroidRuntime(537):11 ...更多
06-11 21:31:20.510:E / AndroidRuntime(537):java.io.FileNotFoundException:致/data/data/com.example.namesgenerator/databases/names2.db(没有这样的文件或目录)
06-11 21:31:20.510:E / AndroidRuntime(537):在org.apache.harmony.luni.platform.OSFileSystem.open(本机方法)
06-11 21:31:20.510:E / AndroidRuntime(537):在dalvik.system.BlockGuard $ WrappedFileSystem.open(BlockGuard.java:232)
06-11 21:31:20.510:E / AndroidRuntime(537):在java.io.FileOutputStream中的<&初始化GT;(FileOutputStream.java:94)
06-11 21:31:20.510:E / AndroidRuntime(537):在java.io.FileOutputStream中的<&初始化GT;(FileOutputStream.java:66)
06-11 21:31:20.510:E / AndroidRuntime(537):在com.example.namesgenerator.dbAdapter.assetCop​​yDatabase(dbAdapter.java:117)
06-11 21:31:20.510:E / AndroidRuntime(537):在com.example.namesgenerator.dbAdapter.assetOpenDatabase(dbAdapter.java:106)
06-11 21:31:20.510:E / AndroidRuntime(537):14 ...更多


解决方案

在您的手机转到

 设置 - >储存

和验证,如果只未选择海量存储的标志。当它被选中,你的设备就无法访问以进行调试(还是其他目的一般)SD卡。这是相当有可能你的文件的创建失败的原因。

) I am beginner in Android programming and I have little problem. I wrote simple DataBase app(I have ready Data Base) and I need to copy it to app folder on phone. I have method for this. When I run app on emulator, all works but when I copy this app to my phone, app have crash. Can you help me? This is dbAdapter:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class dbAdapter {
private static final String DB_NAME = "names2.db";
private static final int DB_VERSION = 1;
private static final String DB_TABLE = "names";
String DB_PATH;
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final int NAME_COLUMN = 1;
public static final String KEY_SEX = "sex";
public static final int SEX_COLUMN = 2;
public static final String KEY_RACE = "race";
public static final int RACE_COLUMN = 3;
private SQLiteDatabase db;
private final Context context;
private DatabaseHelper myDatabaseHelper;

public dbAdapter(Context _context){
    context = _context;
    //DB_PATH = context.getApplicationContext().getFilesDir().getPath();

    DB_PATH = context.getFilesDir().getPath()+"/databases/";
    myDatabaseHelper = new DatabaseHelper(_context,DB_NAME,null,DB_VERSION);
}

public dbAdapter open(){
    db = myDatabaseHelper.getReadableDatabase();
    return this;
}

public void close(){
    db.close();
}

public long insert(rekord _rekord){
    ContentValues newRekord = new ContentValues();
    newRekord.put(KEY_NAME, _rekord.getName());
    newRekord.put(KEY_SEX, _rekord.getSex());
    newRekord.put(KEY_RACE, _rekord.getRace());
    return db.insert(DB_TABLE, null, newRekord);
}
public String getData(String sex, String race) {
    Cursor mycursor = db.rawQuery("SELECT name FROM names WHERE sex ="+sex+" AND race="+race+" ORDER BY RANDOM() LIMIT 1", null);
      if(mycursor.moveToFirst()){ //Edited based on suggestion from SAM
          String strCatName = mycursor.getString(mycursor.getColumnIndex(KEY_NAME));
                  return strCatName;
        } else {
        return null;
         }
      // return db.rawQuery("SELECT name FROM names WHERE sex =1 ORDER BY RANDOM() LIMIT 1", new String[]{"name"});
    }
public void copyDatabase() throws IOException{ //niby kopiowanie bazy danych. Nie wiem czy dobre bo nie chce działać

    //Open your local db as the input stream
    InputStream myInput = context.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
public void del(){
    File file = new File(DB_PATH+DB_NAME);
    if(file.exists())
        file.delete();
}



public SQLiteDatabase assetOpenDatabase() {
    File dbFile = context.getDatabasePath(DB_NAME);
    if (!dbFile.exists()) {
        try {
            assetCopyDatabase(dbFile);
        } catch (IOException e) {
            throw new RuntimeException("Error creating source database", e);
        }
    }

    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}

private void assetCopyDatabase(File dbFile) throws IOException {
    InputStream is = context.getAssets().open(DB_NAME);
    OutputStream os = new FileOutputStream(dbFile);

    byte[] buffer = new byte[1024];
    while (is.read(buffer) > 0) {
        os.write(buffer);
    }

    os.flush();
    os.close();
    is.close();
}
//--------------------------------------------------------
class DatabaseHelper extends SQLiteOpenHelper{
    public DatabaseHelper(Context context, String name, CursorFactory factory, int version){
        super(context,name,factory,version);
    }
    @Override
    public void onCreate(SQLiteDatabase _db){
        //_db.execSQL("CREATE TABLE names(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, sex NUMERIC NOT NULL, race NUMERIC NOT NULL)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVer, int newVer){
        _db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE);
        onCreate(_db);
    }
}
}

And that's how I copying file:

dbAdapter baza = new dbAdapter(this);
baza.copyDatabase();

or baza.assetOpenDatabase(); Same effect.


Ehh, now I have error on emulator too...
06-11 21:31:20.510: E/AndroidRuntime(537): FATAL EXCEPTION: main
06-11 21:31:20.510: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.namesgenerator/com.example.namesgenerator.sex}: java.lang.RuntimeException: Error creating source database
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:123)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-11 21:31:20.510: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
06-11 21:31:20.510: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:507)
06-11 21:31:20.510: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 21:31:20.510: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 21:31:20.510: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
06-11 21:31:20.510: E/AndroidRuntime(537): Caused by: java.lang.RuntimeException: Error creating source database
06-11 21:31:20.510: E/AndroidRuntime(537):  at com.example.namesgenerator.dbAdapter.assetOpenDatabase(dbAdapter.java:108)
06-11 21:31:20.510: E/AndroidRuntime(537):  at com.example.namesgenerator.sex.onCreate(sex.java:39)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-11 21:31:20.510: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-11 21:31:20.510: E/AndroidRuntime(537):  ... 11 more
06-11 21:31:20.510: E/AndroidRuntime(537): Caused by: java.io.FileNotFoundException: /data/data/com.example.namesgenerator/databases/names2.db (No such file or directory)
06-11 21:31:20.510: E/AndroidRuntime(537):  at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
06-11 21:31:20.510: E/AndroidRuntime(537):  at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
06-11 21:31:20.510: E/AndroidRuntime(537):  at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
06-11 21:31:20.510: E/AndroidRuntime(537):  at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
06-11 21:31:20.510: E/AndroidRuntime(537):  at com.example.namesgenerator.dbAdapter.assetCopyDatabase(dbAdapter.java:117)
06-11 21:31:20.510: E/AndroidRuntime(537):  at com.example.namesgenerator.dbAdapter.assetOpenDatabase(dbAdapter.java:106)
06-11 21:31:20.510: E/AndroidRuntime(537):  ... 14 more

解决方案

On your phone go to

Settings->Storage

and verify if the flag for Mass storage only isn't selected. When it is selected, your Device can't access the SD card for debugging purposes (nor other purposes in general). That's quite likely the reason the creation of your file is failing.

这篇关于在电话,但不是在模拟器应用程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆