我的应用程序中的权限有问题.看起来我的应用程序没有权限在存储上创建文件.
String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos; try { fos = getApplicationContext() .openFileOutput(FILENAME,Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); }
在我的主类onCreate处理程序中,我总是得到:
06-02 13:23:52.996: WARN/ApplicationContext(6278): Unable to create files directory 06-02 13:23:54.203: WARN/System.err(6278): java.lang.NullPointerException 06-02 13:23:54.226: WARN/System.err(6278): at android.app.ContextImpl.openFileOutput(ContextImpl.java:464) 06-02 13:23:54.234: WARN/ActivityManager(2470): Activity idle timeout for HistoryRecord{485ebe10 com.servision.svclient/.Main} 06-02 13:23:54.257: WARN/System.err(6278): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158) 06-02 13:23:54.273: WARN/System.err(6278): at com.servision.svclient.Main.onCreate(Main.java:59) 06-02 13:23:54.277: WARN/System.err(6278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-02 13:23:54.285: WARN/System.err(6278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 06-02 13:23:54.293: WARN/System.err(6278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-02 13:23:54.296: WARN/System.err(6278): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-02 13:23:54.304: WARN/System.err(6278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-02 13:23:54.312: WARN/System.err(6278): at android.os.Handler.dispatchMessage(Handler.java:99) 06-02 13:23:54.320: WARN/System.err(6278): at android.os.Looper.loop(Looper.java:123) 06-02 13:23:54.324: WARN/System.err(6278): at android.app.ActivityThread.main(ActivityThread.java:4627) 06-02 13:23:54.332: WARN/System.err(6278): at java.lang.reflect.Method.invokeNative(Native Method) 06-02 13:23:54.336: WARN/System.err(6278): at java.lang.reflect.Method.invoke(Method.java:521) 06-02 13:23:54.343: WARN/System.err(6278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 06-02 13:23:54.355: WARN/System.err(6278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-02 13:23:54.359: WARN/System.err(6278): at dalvik.system.NativeStart.main(Native Method)
作为回应.
我相信它在我的项目定义中非常简单.但我无法弄清楚它是什么.
如果我创建另一个项目并将相同的代码放入其中,它可以正常工作.
我的清单文件如下所示:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.servision.svclient" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> <application android:icon="@drawable/main" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".Main" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".StreamActivity" android:label="@string/app_name"/> <activity android:name=".MainActivity"/> <activity android:name=".GatewayList"/> <activity android:name=".StatisticsActivity"/> <activity android:name=".SettingsActivity"/> <activity android:name=".MapsActivity"/> <activity android:name=".AboutActivity"/> </application> <uses-sdk android:minSdkVersion="8" /> </manifest>
解决方法
设备管理=>右键单击Device =>卸载App =>重新运行.