gpt4 book ai didi

android - 文件创建错误中的 java.lang.NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 09:40:58 28 4
gpt4 key购买 nike

我试图将输出音频文件保存在一个单独的新文件夹中,但是当我尝试启动该应用程序时它崩溃了:

我的文件创建代码如下:

onCreate() aof mainActivity 中:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button)findViewById(R.id.button3);
stop=(Button)findViewById(R.id.button2);
record=(Button)findViewById(R.id.button);


stop.setEnabled(false);
play.setEnabled(false);

final File path =
Environment.getExternalStoragePublicDirectory
(
//Environment.DIRECTORY_PICTURES
//Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DCIM + "/Utkarshrecord/"
);

// Make sure the sound directory exists.
if(!path.exists())
{
path.mkdirs();
}

try {
file.createTempFile("sound", ".3gp", path);

myAudioRecorder=new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setOutputFile(file.getAbsolutePath());

} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

录制停止播放三个按钮:

记录的代码:

record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
}

catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

record.setEnabled(false);
stop.setEnabled(true);

Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});

停止的代码:

 stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
try{
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;




}catch(Exception e){
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
stop.setEnabled(false);
play.setEnabled(true);
}
});

错误如下:

10-05 14:44:04.910: E/AndroidRuntime(31147): FATAL EXCEPTION: main
10-05 14:44:04.910: E/AndroidRuntime(31147): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sensorlist/com.example.sensorlist.MainActivity}: java.lang.NullPointerException
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.os.Handler.dispatchMessage(Handler.java:107)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.os.Looper.loop(Looper.java:194)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.main(ActivityThread.java:5371)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.lang.reflect.Method.invoke(Method.java:525)
10-05 14:44:04.910: E/AndroidRuntime(31147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-05 14:44:04.910: E/AndroidRuntime(31147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-05 14:44:04.910: E/AndroidRuntime(31147): at dalvik.system.NativeStart.main(Native Method)
10-05 14:44:04.910: E/AndroidRuntime(31147): Caused by: java.lang.NullPointerException
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.File.fixSlashes(File.java:185)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.File.<init>(File.java:134)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
10-05 14:44:04.910: E/AndroidRuntime(31147): at com.example.sensorlist.MainActivity.onCreate(MainActivity.java:64)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.Activity.performCreate(Activity.java:5122)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
10-05 14:44:04.910: E/AndroidRuntime(31147): ... 11 more

我应该怎么做才能删除它?

最佳答案

因为你犯了一些非常愚蠢的错误!

错误一:

file.createTempFile("sound", ".3gp", path);

对于 static ref 应该是

File.createTempFile("sound", ".3gp", path);

并且它应该存储在您的文件中,该文件直到现在都是null!

第二个错误:

在播放按钮中:改成

 m.setDataSource(file.getAbsolutePath());

关于android - 文件创建错误中的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39870088/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com