2012-02-16 34 views
0

从openFileOutput发生NullPointerException。 context.getPackageName()= android,这段代码是框架。 我不知道这个问题,背景或任何东西... 任何人都可以帮我吗?在openFileOutput Android中,NullPointerException

次使用的联系人应用

2月2日至16日:06:42.449:I/VCardImport(2255):电子名片缓存线程开始运行。

二月2日至16日:06:42.449:E/VCardImport(2255):文件名:import_tmp_0.vcf

二月2日至16日:06:42.449:E/VCardImport(2255):文件:/数据/数据/ com.android.contacts /文件/ import_tmp_0.vcf

-My源,框架

1月2日至16日:11:00.525:E/PstContactCreator(549):文件名:import_tmp_0.vcf

02-16 01:11:00.525:W/ApplicationContext(549):无法创建文件目录文件

一月2日至16日:11:00.525:E/PstContactCreator(549):文件:import_tmp_0.vcf

我的源的上下文是 '机器人',当我使用功能,这是getFileStreamPath,openFileOutput,我无法创建文件。我认为情况会导致问题。我想知道是什么原因导致问题以及如何解决问题。 context.getFileStreamPath(filename); context.openFileOutput(filename,Context.MODE_PRIVATE).getChannel();

私人乌里CopyTo从(最终乌里sourceUri,字符串文件名)抛出IOException异常{

Log.i(LOG_TAG, String.format("Copy a Uri to app local storage (%s -> %s)", 
      sourceUri, filename)); 
    final Context context = mContext.get(); 
    final ContentResolver resolver = context.getContentResolver(); 
    ReadableByteChannel inputChannel = null; 
    WritableByteChannel outputChannel = null; 
    Uri destUri = null; 
    try { 
     inputChannel = Channels.newChannel(resolver.openInputStream(sourceUri)); 
     destUri = Uri.parse(context.getFileStreamPath(filename).toURI().toString()); 
     outputChannel = context.**openFileOutput**(filename, Context.MODE_PRIVATE).getChannel(); 

     final ByteBuffer buffer = ByteBuffer.allocateDirect(8192); 

     while (inputChannel.read(buffer) != -1) { 
      buffer.flip(); 
      outputChannel.write(buffer); 
      buffer.compact(); 
     } 

     buffer.flip(); 

     while (buffer.hasRemaining()) { 
      outputChannel.write(buffer); 
     } 

    } finally { 
     if (inputChannel != null) { 
      try { 
       inputChannel.close(); 
      } catch (IOException e) { 
       Log.w(LOG_TAG, "Failed to close inputChannel."); 
      } 
     } 
     if (outputChannel != null) { 
      try { 
       outputChannel.close(); 
      } catch(IOException e) { 
       Log.w(LOG_TAG, "Failed to close outputChannel"); 
      } 
     } 
    } 
    return destUri; 
} 

02-16 01:11:00.455:I/PstManagerService(549):onsetrecordcmd启动

02-16 01:11:00.455:I/PstManagerService(549):f.exists

1月2日至16日:11:00.455:I/PstManagerService(549):后creteNewFile

1月2日至16日:11:00.465 :W/PstContact创建者(549):pst联系开始!

1月2日至一十六日:11:00.465:E/PstContactCreator(549):文件名:import_tmp_0.vcf

1月2日至一十六日:11:00.465:W/ApplicationContext对象(549):无法创建文件目录文件

1月2日至一十六日:11:00.475:E/PstContactCreator(549):文件:import_tmp_0.vcf

1月2日至一十六日:11:00.475:I/PstContactCreator(549):一个URI复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)

1月2日至十六日:11:00.475:I/PstContactCreator(549):dcshin 111

1月2日至十六日:11:00.475:E/PstContactCreator(549):inputChannel: java.nio.channels.Channels $ InputStreamChannel @ 419ca7a8

02-16 01:11:00。475:I/PstContactCreator(549):dcshin 222

1月2日至16日:11:00.485:W/ApplicationContext的(549):无法创建文件目录中的文件

1月2日至16日:11:00.495: E/PstContactCreator(549):destUri:文件:/import_tmp_0.vcf

1月2日至一十六日:11:00.495:I/PstContactCreator(549):dcshin 333

1月2日至一十六日:11:00.495: E/PstContactCreator(549):getPackageName:android

02-16 01:11:00.495:E/PstContactCreator(549):getPackag eCodePath:空

一月2日至16日:11:00.505:E/PstContactCreator(549):getPackageResourcePath:空

一月2日至16日:11:00.505:W/ApplicationContext对象(549):无法创建文件目录中的文件

1月2日至一十六日:11:00.505:E/PstContactCreator(549):显示java.lang.NullPointerException

1月2日至一十六日:11:00.505:I/PstContactCreator(549):成品缓存名片。

1月2日至16日:11:00.515:I/PstManagerService(549):onsetrecordcmd启动

1月2日至16日:11:00.515:I/PstManagerService(549):f.exists

02 -16 01:11:00.515:I/PstManagerService(549):after creteNewFile

02-16 01:11:00.525:W/PstContactCreator(549):pst contact start!

1月2日至一十六日:11:00.525:E/PstContactCreator(549):文件名:import_tmp_0.vcf

1月2日至一十六日:11:00.525:W/ApplicationContext对象(549):无法创建文件目录文件

1月2日至一十六日:11:00.525:E/PstContactCreator(549):文件:import_tmp_0.vcf

1月2日至一十六日:11:00.525:I/PstContactCreator(549):一个URI复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)

1月2日至十六日:11:00.525:I/PstContactCreator(549):dcshin 111

1月2日至十六日:11:00.525:E/PstContactCreator(549):inputChannel: java.nio.channels.Channels $ InputStreamChannel @ 419b0018

1月2日至16日:11:00.525:I/PstContactCreator(549):dcshin 222

1月2日至16日:11:00.525:W/ApplicationContext对象(549):无法创建文件目录文件

02-16 01:11:00.525:E/ThermalDaemon(2916):最大CPU [1]频率918000 KHz

1月2日至16日:11:00.525:E/PstContactCreator(549):destUri:文件:/import_tmp_0.vcf

1月2日至16日:11:00.525:I/PstContactCreator(549):333 dcshin

02-16 01:11:00。525:E/PstContactCreator(549):getPackageName:机器人

1月2日至16日:11:00.525:E/PstContactCreator(549):getPackageCodePath:空

1月2日至16日:11:00.525:E/PstContactCreator(549):getPackageResourcePath:空

一月2日至16日:11:00.525:W/ApplicationContext对象(549):无法创建文件目录下的文件

一月2日至16日:11:00.525:E/PstContactCreator (549):java.lang.NullPointerException

02-16 01:11:00.525:I/P stContactCreator(549):完成缓存电子名片。

1月2日至16日:11:00.535:I/PstManagerService(549):onsetrecordcmd启动

1月2日至16日:11:00.535:I/PstManagerService(549):f.exists

02 -16 01:11:00.535:I/PstManagerService(549):after creteNewFile

02-16 01:11:00.535:W/PstContactCreator(549):pst contact start!

1月2日至一十六日:11:00.535:E/PstContactCreator(549):文件名:import_tmp_0.vcf

1月2日至一十六日:11:00.535:W/ApplicationContext对象(549):无法创建文件目录文件

1月2日至一十六日:11:00.535:E/PstContactCreator(549):文件:import_tmp_0.vcf

1月2日至一十六日:11:00.535:I/PstContactCreator(549):一个URI复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)

1月2日至十六日:11:00.535:I/PstContactCreator(549):dcshin 111

1月2日至十六日:11:00.535:E/PstContactCreator(549):inputChannel: java.nio.channels.Channels $ InputStreamChannel @ 414d6ca8

1月2日至16日:11:00.535:I/PstContactCreator(549):dcshin 222

1月2日至16日:11:00.535:W/ApplicationContext对象(549):无法创建文件目录文件

1月2日至一十六日:11:00.545:E/PstContactCreator(549):destUri:文件:/import_tmp_0.vcf

02-16 01:11:00.545:I/PstContactCreator(549):333 dcshin

02-16 01:11:00.545:E/PstContactCreator(549):getPackageName:机器人

02-16 01:11:00.545:E/PstContactCreator(549):getPackageCodePath:空

02-16 01:11:00.545:E/PstContactCreator(549):getPackageResourcePath:空

02-16 1点11分: 00.545:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.545:E/PstContactCreator(549):java.lang。NullPointerException

02-16 01:11:00.545:I/PstContactCreator(549):完成缓存vCard。

1月2日至16日:11:00.545:I/PstManagerService(549):onsetrecordcmd启动

1月2日至16日:11:00.545:I/PstManagerService(549):f.exists

02 -16 01:11:00.555:I/PstManagerService(549):after creteNewFile

02-16 01:11:00.555:W/PstContactCreator(549):pst contact start!

1月2日至一十六日:11:00.555:E/PstContactCreator(549):文件名:import_tmp_0.vcf

1月2日至一十六日:11:00.555:W/ApplicationContext对象(549):无法创建文件目录文件

1月2日至一十六日:11:00.555:E/PstContactCreator(549):文件:import_tmp_0.vcf

1月2日至一十六日:11:00.555:I/PstContactCreator(549):一个URI复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)

1月2日至十六日:11:00.555:I/PstContactCreator(549):dcshin 111

1月2日至十六日:11:00.555:E/PstContactCreator(549):inputChannel: java.nio.channels.Channels $ InputStreamChannel @ 41a20ff8

1月2日至16日:11:00.555:I/PstContactCreator(549):dcshin 222

1月2日至16日:11:00.555:W/ApplicationContext对象(549):无法创建文件目录文件

1月2日至一十六日:11:00.555:E/PstContactCreator(549):destUri:文件:/import_tmp_0.vcf

02-16 01:11:00.555:I/PstContactCreator(549):333 dcshin

02-16 01:11:00.555:E/PstContactCreator(549):getPackageName:机器人

02-16 01:11:00.555:E/PstContactCreator(549):getPackageCodePath:空

02-16 01:11:00.555:E/PstContactCreator(549):getPackageResourcePath:空

02-16 1点11分: 00.555:W/ApplicationContext(549):无法创建文件目录文件

1月2日至16日:11:00.555:E/PstContactCreator(549):显示java.lang.NullPointerException

1月2日至16日:11:00.555:I/PstContactCreator(549):成品缓存电子名片。

1月2日至一十六日:11:00.565:I/DiagJNIInterface(1791):将要发送的消息

1月2日至一十六日:11:00.565:d/ShutdownThread(549):通报线程开始关机 longPressBehavior = 1 02-16 01:11:00.615:I/ShutdownThread(549):发送关机广播...

回答

0

首先检查你的android清单文件。在权限中,您应该添加WRITE_EXTERNAL:存储 或写入联系人权限。我看到你在处理

+0

我的源框架。context.getPackageName()=机器人。框架需要权限吗? – user1212704 2012-02-16 01:38:22

+0

不需要权限我想,你可以发布你的新catlog,有一个更好的想法,哪些错误仍然存​​在 – 2012-02-16 01:49:07

+0

我不知道,如何发布.logcat添加。 – user1212704 2012-02-16 02:02:41

0

你的应用拥有包名android?这是错误的。将其更改为类似com.yourname

0

我有一个类似的问题(NullPointerException看起来是来自openFileOutput),其中我没有首先传递上下文创建了一个Java类。

getApplicationContext().openFileOutput(FILENAME, Context.MODE_PRIVATE); 

没有工作,(即使编译正常),所以我改变它到下面。 (注ctx.openFileOutput。

public ClassName(Context ctx){ 
    FileOutputStream fos; 
    try { 
     fos = ctx.openFileOutput(FILENAME, Context.MODE_PRIVATE); 
     fos.write(("Date created: " + SimpleDateFormat.getDateInstance().format(now) + "\n").getBytes()); 
     fos.close(); 
    } 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

虽然这可能无法解决您的问题,我就会把它在这里情况下,它可以帮助别人。

相关问题