简体   繁体   中英

Android - Saving data on external storage

I have an audio file named "s1" in my raw folder in my project, and I put the following code onto a button on the main screen, when I run my emulator and press run, it crashes the program. If anyone can provide any help or solutions I would be eternally grateful

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 output:

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

We can't do anything if you don't provide the logcat with the exception!

A completely blind guess would be that you are missing the permission.

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

Edit

Solution based on what I've seen of the code

It seems like your MainActivity is being called from a click listener set via XML, with an action saveData.

I noted that on line 44 you have a method called savaData with no parameters. A click listener called from XML needs a view, so the fix is to change line 44 in your pastebin to this:

  public void saveData(View view) {

Make sure you note the a changed to an e on the word save!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM