android studio java.lang.NullPointerException

时间:2015-09-15 21:09:43

标签: android-studio

所以我正在开发需要用SD卡写的app,当我尝试写入文件时,它说:java.lang.NullPointerException 这是我的代码:

public void goWrite() {
    File sdPath = Environment.getExternalStorageDirectory();
    sdPath = new File(sdPath.getAbsolutePath() + "/" + DIR);
    sdPath.mkdir();
    Toast.makeText(this ,"" + sdPath ,Toast.LENGTH_LONG).show();
    File sdFile = new File(sdPath ,filename);
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(sdFile));
        bw.write("Correct answers - " + correct + "\nincorrect answers - " + incorrect);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我的清单     

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".indentification"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".trivia"
        android:label="@string/title_activity_indentification" >
    </activity>
    <activity
        android:name=".Summary"
        android:label="@string/title_activity_summary" >
    </activity>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

</application>

我需要做什么? 谢谢你的帮助:D

0 个答案:

没有答案