Android - 在外部存储上保存数据

时间:2014-01-25 02:26:18

标签: android

我的项目中的原始文件夹中有一个名为“s1”的音频文件,我将以下代码放在主屏幕上的按钮上,当我运行模拟器并按下运行时,它会使程序崩溃。如果有人能提供任何帮助或解决方案,我将永远感激

public void fileSetup() {

    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
    String name1 = "Zen1";
    File file1 = new File(path, name1 + ".mp3");
    path.mkdirs();
    InputStream is1 = getResources().openRawResource(R.raw.s1); 
    try {
         OutputStream os1 = new FileOutputStream(file1);
         byte[] data1 = new byte [is1.available()]; 
         is1.read(data1);
         os1.write(data1);
         is1.close();
         os1.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

Logcat输出:

01-24 23:00:42.137: E/AndroidRuntime(1064): FATAL EXCEPTION: main
01-24 23:00:42.137: E/AndroidRuntime(1064): java.lang.IllegalStateException: Could not find a method saveData(View) in the activity class com.malthorn.zenstatemeditation.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.view.View$1.onClick(View.java:3620)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.view.View.performClick(View.java:4240)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.view.View$PerformClick.run(View.java:17721)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.os.Handler.handleCallback(Handler.java:730)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.os.Looper.loop(Looper.java:137)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at java.lang.reflect.Method.invokeNative(Native Method)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at java.lang.reflect.Method.invoke(Method.java:525)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at dalvik.system.NativeStart.main(Native Method)
01-24 23:00:42.137: E/AndroidRuntime(1064): Caused by: java.lang.NoSuchMethodException: saveData [class android.view.View]
01-24 23:00:42.137: E/AndroidRuntime(1064):     at java.lang.Class.getConstructorOrMethod(Class.java:423)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at java.lang.Class.getMethod(Class.java:787)
01-24 23:00:42.137: E/AndroidRuntime(1064):     at android.view.View$1.onClick(View.java:3613)
01-24 23:00:42.137: E/AndroidRuntime(1064):     ... 11 more

1 个答案:

答案 0 :(得分:1)

如果您没有为logcat提供异常,我们无法做任何事情!

完全盲目的猜测是你错过了许可。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

修改

基于我所看到的代码

的解决方案

看起来你的MainActivity是通过XML从一个点击监听器集调用的,带有一个动作saveData。

我注意到在第44行你有一个名为savaData的方法,没有参数。从XML调用的单击侦听器需要一个视图,因此修复方法是将pastebin中的第44行更改为:

  public void saveData(View view) {

请务必在单词save上注意 a 更改为 e