我在我的項目中的原始文件夾中有一個名爲「s1」的音頻文件,並且當我運行我的模擬器並按下運行時,將以下代碼放到主屏幕上的按鈕上,它使程序崩潰。如果任何人都可以提供任何幫助或解決方案,我將永遠感激Android - 在外部存儲器上保存數據
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
你將不得不提供超過「它崩潰的程序」,以獲得任何真正的幫助。您必須提供logcat輸出,以便我們可以確切地看到發生了什麼。 – ArmaAK
發佈您的XML文件 – Apoorv