Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android NullPointerException/IllegalStateException OutputStream.write()_Android_Nullpointerexception - Fatal编程技术网

Android NullPointerException/IllegalStateException OutputStream.write()

Android NullPointerException/IllegalStateException OutputStream.write(),android,nullpointerexception,Android,Nullpointerexception,我的备份当前数据库的方法似乎有问题。方法如下: 当使用运行4.2.2的Nexus4时,该方法工作正常,但当使用仿真器模拟版本API15时,会发生错误。发生错误的行是os.write(),谢谢 public void backupdatebase()引发IOException{ File File=context.getDatabasePath(数据库名称); FileInputStream fis=null; 试一试{ fis=新文件输入流(文件); }catch(filenotfounde异常

我的备份当前数据库的方法似乎有问题。方法如下:

当使用运行4.2.2的Nexus4时,该方法工作正常,但当使用仿真器模拟版本API15时,会发生错误。发生错误的行是os.write(),谢谢

public void backupdatebase()引发IOException{
File File=context.getDatabasePath(数据库名称);
FileInputStream fis=null;
试一试{
fis=新文件输入流(文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}
File appDir=新文件(Environment.getExternalStorageDirectory()+“/AppName/”;
appDir.mkdir();
File outputDB=新文件(appDir,“BackupFile.File”);
//字符串outputDB=Environment.getExternalStorageDirectory()+“/AppName/BackupFile.file”;
OutputStream os=null;
试一试{
os=新文件输出流(outputDB);
}catch(filenotfounde异常){
e、 printStackTrace();
}
//将字节从输入文件传输到输出文件
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=fis.read(缓冲区))>0){

write(缓冲区,0,长度);//在我看来,
os
目前是空的。如果是这样,那么创建
FileOutputStream
会抛出一个
FileNotFoundException
。如果有日志,你应该发布任何日志

还要确保设置
读取外部存储
写入外部存储

我的建议是检查操作系统当前是否为空,然后再写,因为文件打开可能会失败


另外,我不会在实际设备上依赖emulator,因为它只是模拟系统。如果您有任何可以测试的真实设备,请使用它们。

WRITE\u EXTERNAL\u STORAGE隐式允许读取,您可以跳过READ\u EXTERNAL\u STORAGE权限。对于其余的,我会+1。谢谢您的回复。我会接受您的建议-检查null然后写。我的权限已经有读/写外部存储。奇怪的是,它在我的设备上工作,但在模拟器上不工作。此外,在此之前没有日志,没有FileNotFoundException或任何东西。运行API 4.3的模拟器工作,但没有API15Edit!只是添加了空检查,它工作了!-出于某种原因,没有空检查未引发FileNotFoundException,但通过null检查引发了此异常。问题已解决:在emulator设置中,您需要正确指定外部存储(“SD卡”)的大小。默认情况下,“外部存储”字段为空,这可能意味着没有这样的设备,即使在清单中授予了权限,也会抛出EACCES
public void backupDatabase() throws IOException {

    File file = context.getDatabasePath(DATABASE_NAME);
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    File appDir = new File( Environment.getExternalStorageDirectory() + "/AppName/");
    appDir.mkdir();
    File outputDB = new File(appDir, "BackupFile.file");
    //String outputDB = Environment.getExternalStorageDirectory() +"/AppName/BackupFile.file";
    OutputStream os = null;
    try {
        os = new FileOutputStream(outputDB);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0) {
           os.write(buffer, 0, length); //<---Error.... (Only on emulator)
    }
    // Close the streams
    os.flush();
    os.close();
    fis.close();

    Toast.makeText(context, "Data successfully backed up!", Toast.LENGTH_SHORT).show();
}


01-18 18:57:38.077: E/ACRA(1961): PACKAGE_NAME fatal error : Could not execute method of the activity
01-18 18:57:38.077: E/ACRA(1961): java.lang.IllegalStateException: Could not execute method of the activity
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View$1.onClick(View.java:3044)
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View.performClick(View.java:3511)
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View$PerformClick.run(View.java:14105)
01-18 18:57:38.077: E/ACRA(1961):   at android.os.Handler.handleCallback(Handler.java:605)
01-18 18:57:38.077: E/ACRA(1961):   at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 18:57:38.077: E/ACRA(1961):   at android.os.Looper.loop(Looper.java:137)
01-18 18:57:38.077: E/ACRA(1961):   at android.app.ActivityThread.main(ActivityThread.java:4424)
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invoke(Method.java:511)
01-18 18:57:38.077: E/ACRA(1961):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 18:57:38.077: E/ACRA(1961):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 18:57:38.077: E/ACRA(1961):   at dalvik.system.NativeStart.main(Native Method)
01-18 18:57:38.077: E/ACRA(1961): Caused by: java.lang.reflect.InvocationTargetException
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invoke(Method.java:511)
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View$1.onClick(View.java:3039)
01-18 18:57:38.077: E/ACRA(1961):   ... 11 more
01-18 18:57:38.077: E/ACRA(1961): Caused by: java.lang.NullPointerException
01-18 18:57:38.077: E/ACRA(1961):   at     PACKAGE_NAME.backupDatabase(DbHelper.java:85)