java.lang.NullPointerException 文件创建错误
java.lang.NullPointerException in File creation error
我试图将输出音频文件保存在一个单独的新文件夹中,但是当我尝试启动应用程序时它崩溃了:
我的文件创建代码如下:
在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.access0(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());
我试图将输出音频文件保存在一个单独的新文件夹中,但是当我尝试启动应用程序时它崩溃了:
我的文件创建代码如下:
在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.access0(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());