这是我第一次在这里提问,我对Android应用程序编程很陌生。我所拥有的是一个按钮,当按下该按钮时,使用reading_wav()读取存储在手机上的文件。但是,按下按钮后,我的应用程序停止响应。我知道这个功能正常,因为我已经在一个单独的java项目上测试了它。 感谢
public class CheckWavFiles2 extends ActionBarActivity {// No runnable
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.check_wav2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_check_wav2,
container, false);
return rootView;
}
}
private static final String LOG_TAG = "CheckWavFiles2";
private static String mFileName = null;
int i = 0;
private NormalButton mNormalButton = null;
class NormalButton extends Button {
String mClassifyNormal = "Normal.wav";
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
setText("Normal.wav");
onNormal(mClassifyNormal);
}
};
public NormalButton(Context ctx) {
super(ctx);
setText("Normal.wav");
setOnClickListener(clicker);
}
}
private void onNormal(String fName) {
if (fName == "Normal.wav") {
readNormal();
} else {
}
}
private void readNormal() {
new Thread() {
@Override
public void run() {
reading_wav sampleReader = null;
try {
sampleReader = new reading_wav(new File(mFileName));
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public CheckWavFiles2() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.wav";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mNormalButton = new NormalButton(this);
ll.addView(mNormalButton, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0));
setContentView(ll);
}
}
Logcat输出:
12-07 01:47:03.261: D/libEGL(20770): loaded /system/lib/egl/libEGL_mali.so
12-07 01:47:03.276: D/libEGL(20770): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-07 01:47:03.281: D/libEGL(20770): loaded /system/lib/egl/libGLESv2_mali.so
12-07 01:47:03.291: E/(20770): Device driver API match
12-07 01:47:03.291: E/(20770): Device driver API version: 20
12-07 01:47:03.291: E/(20770): User space API version: 20
12-07 01:47:03.291: E/(20770): mali: REVISION=Linux-r3p2-01rel2 BUILD_DATE=Mon Sep 2 14:16:28 KST 2013
12-07 01:47:03.311: D/OpenGLRenderer(20770): Enabling debug mode 0
12-07 01:47:13.261: W/dalvikvm(20770): VFY: unable to resolve exception class 1301 (Ljavax/sound/sampled/UnsupportedAudioFileException;)
12-07 01:47:13.261: W/dalvikvm(20770): VFY: unable to find exception handler at addr 0x11
12-07 01:47:13.261: W/dalvikvm(20770): VFY: rejected Lcom/example/stethoscope2/CheckWavFiles2$1;.run ()V
12-07 01:47:13.261: W/dalvikvm(20770): VFY: rejecting opcode 0x0d at 0x0011
12-07 01:47:13.261: W/dalvikvm(20770): VFY: rejected Lcom/example/stethoscope2/CheckWavFiles2$1;.run ()V
12-07 01:47:13.261: W/dalvikvm(20770): Verifier rejected class Lcom/example/stethoscope2/CheckWavFiles2$1;
12-07 01:47:13.261: D/AndroidRuntime(20770): Shutting down VM
12-07 01:47:13.261: W/dalvikvm(20770): threadid=1: thread exiting with uncaught exception (group=0x41a24930)
12-07 01:47:13.271: E/AndroidRuntime(20770): FATAL EXCEPTION: main
12-07 01:47:13.271: E/AndroidRuntime(20770): java.lang.VerifyError: com/example/stethoscope2/CheckWavFiles2$1
12-07 01:47:13.271: E/AndroidRuntime(20770): at com.example.stethoscope2.CheckWavFiles2.readNormal(CheckWavFiles2.java:94)
12-07 01:47:13.271: E/AndroidRuntime(20770): at com.example.stethoscope2.CheckWavFiles2.onNormal(CheckWavFiles2.java:84)
12-07 01:47:13.271: E/AndroidRuntime(20770): at com.example.stethoscope2.CheckWavFiles2.access$0(CheckWavFiles2.java:82)
12-07 01:47:13.271: E/AndroidRuntime(20770): at com.example.stethoscope2.CheckWavFiles2$NormalButton$1.onClick(CheckWavFiles2.java:71)
12-07 01:47:13.271: E/AndroidRuntime(20770): at android.view.View.performClick(View.java:4439)
12-07 01:47:13.271: E/AndroidRuntime(20770): at android.view.View$PerformClick.run(View.java:18370)
12-07 01:47:13.271: E/AndroidRuntime(20770): at android.os.Handler.handleCallback(Handler.java:725)
12-07 01:47:13.271: E/AndroidRuntime(20770): at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 01:47:13.271: E/AndroidRuntime(20770): at android.os.Looper.loop(Looper.java:176)
12-07 01:47:13.271: E/AndroidRuntime(20770): at android.app.ActivityThread.main(ActivityThread.java:5299)
12-07 01:47:13.271: E/AndroidRuntime(20770): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 01:47:13.271: E/AndroidRuntime(20770): at java.lang.reflect.Method.invoke(Method.java:511)
12-07 01:47:13.271: E/AndroidRuntime(20770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-07 01:47:13.271: E/AndroidRuntime(20770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-07 01:47:13.271: E/AndroidRuntime(20770): at dalvik.system.NativeStart.main(Native Method)
12-07 01:47:13.296: D/dalvikvm(20770): GC_CONCURRENT freed 153K, 16% free 7779K/9172K, paused 3ms+1ms, total 30ms
12-07 01:47:17.156: D/libEGL(21211): loaded /system/lib/egl/libEGL_mali.so
12-07 01:47:17.171: D/libEGL(21211): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-07 01:47:17.176: D/libEGL(21211): loaded /system/lib/egl/libGLESv2_mali.so
12-07 01:47:17.176: E/(21211): Device driver API match
12-07 01:47:17.176: E/(21211): Device driver API version: 20
12-07 01:47:17.176: E/(21211): User space API version: 20
12-07 01:47:17.176: E/(21211): mali: REVISION=Linux-r3p2-01rel2 BUILD_DATE=Mon Sep 2 14:16:28 KST 2013
12-07 01:47:17.206: D/OpenGLRenderer(21211): Enabling debug mode 0
答案 0 :(得分:0)
某些Android版本并不包含所有Java支持的方法,所以这里的问题是你在方法中使用了一些方法CheckWavFiles2()
Java 支持这种方法但是你用于 android 的sdk级别不支持,所以,如果你使用android-sdk13
(存在于manifest.xml
文件中)作为你的min sdk,请尝试使用android-sdk20
,然后运行您的应用,我认为它会起作用。
在此之前,我认为问题出在Environment.getExternalStorageDirectory().getAbsolutePath();
所以,请尝试改为:
File mPath = new File(Environment.getExternalStorageDirectory(), "/audiorecordtest.wav");
mFileName = mPath.getAbsolutePath();