目前,我有一个按钮,用户必须按此按钮才能将.mp3从raw转换为sdcard。我尝试将void放在我的onCreate void中,但它给了我一个错误。如何更改代码以便它可以在onCreate上工作? LogCat错误:
以下是用于按钮的代码:
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/music/Zen1.mp3");
if (myFile.exists()) {
meditateScreen();
} else {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
String name1 = "Zen1";
String name2 = "Zen2";
String name3 = "Zen3";
String name4 = "ZenPing";
File file1 = new File(path, name1 + ".mp3");
InputStream is1 = getResources().openRawResource(R.raw.s1);
File file2 = new File(path, name2 + ".mp3");
InputStream is2 = getResources().openRawResource(R.raw.s2);
File file3 = new File(path, name3 + ".mp3");
InputStream is3 = getResources().openRawResource(R.raw.s3);
File file4 = new File(path, name4 + ".mp3");
InputStream is4 = getResources().openRawResource(R.raw.ping);
try {
OutputStream os1 = new FileOutputStream(file1);
byte[] data1 = new byte [is1.available()];
is1.read(data1);
os1.write(data1);
is1.close();
os1.close();
//
OutputStream os2 = new FileOutputStream(file2);
byte[] data2 = new byte [is2.available()];
is2.read(data2);
os2.write(data2);
is2.close();
os2.close();
//
OutputStream os3 = new FileOutputStream(file3);
byte[] data3 = new byte [is3.available()];
is3.read(data3);
os3.write(data3);
is3.close();
os3.close();
//
OutputStream os4 = new FileOutputStream(file4);
byte[] data4 = new byte [is4.available()];
is4.read(data4);
os4.write(data4);
is4.close();
os4.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
meditateScreen();
}
这是XML:
02-01 00:30:34.330: E/AndroidRuntime(1607): FATAL EXCEPTION: main
02-01 00:30:34.330: E/AndroidRuntime(1607): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.malthorn.zenstatemeditation/com.malthorn.zenstatemeditation.MeditateScreen}: java.lang.NullPointerException
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.os.Looper.loop(Looper.java:137)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-01 00:30:34.330: E/AndroidRuntime(1607): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 00:30:34.330: E/AndroidRuntime(1607): at java.lang.reflect.Method.invoke(Method.java:525)
02-01 00:30:34.330: E/AndroidRuntime(1607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-01 00:30:34.330: E/AndroidRuntime(1607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-01 00:30:34.330: E/AndroidRuntime(1607): at dalvik.system.NativeStart.main(Native Method)
02-01 00:30:34.330: E/AndroidRuntime(1607): Caused by: java.lang.NullPointerException
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.Activity.findViewById(Activity.java:1853)
02-01 00:30:34.330: E/AndroidRuntime(1607): at com.malthorn.zenstatemeditation.MeditateScreen.<init>(MeditateScreen.java:36)
02-01 00:30:34.330: E/AndroidRuntime(1607): at java.lang.Class.newInstanceImpl(Native Method)
02-01 00:30:34.330: E/AndroidRuntime(1607): at java.lang.Class.newInstance(Class.java:1130)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-01 00:30:34.330: E/AndroidRuntime(1607): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
02-01 00:30:34.330: E/AndroidRuntime(1607): ... 11 more