Java 尝试使用方法重命名录制的文件

Java 尝试使用方法重命名录制的文件,java,android,eclipse,android-mediarecorder,Java,Android,Eclipse,Android Mediarecorder,我正在尝试重命名用MediaRecorder录制的文件。我就是这样做的 首先,我创建了startRecording()和stopRecording方法 public void startRecording (){ recorder = new MediaRecorder(); recorder.reset(); recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); recor

我正在尝试重命名用MediaRecorder录制的文件。我就是这样做的

首先,我创建了startRecording()和stopRecording方法

public void startRecording (){
     recorder = new MediaRecorder();
     recorder.reset();      
     recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
     {
         externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
         externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
         recorder.setOutputFile(externalOutputPath);
     }
     else
     {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
     }
     recorder.setOnErrorListener(errorListener);
     recorder.setOnInfoListener(infoListener);

     try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}
以下是我开始和停止录制的方式:

recBtn.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
                startRecording();
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
                stopRecording();
                nameAlert();
            }
            return true;
        }

    });
} //END OF ONCREATE
因此,我想做的下一件事是,每次我停止按record按钮时,都能够设置输出文件的名称。我试图创建一个新方法,每次停止录制时都会调用它

public void nameAlert() {
    final EditText etFileName = (EditText) findViewById(com.whizzappseasyvoicenotepad.R.id.etFileName);
    Button okBtn = (Button) findViewById(com.whizzappseasyvoicenotepad.R.id.okBtn);

    final Dialog dialog = new Dialog (getApplicationContext(), android.R.style.Theme_Translucent);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(com.whizzappseasyvoicenotepad.R.layout.alert_name);

    okBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            fileName = etFileName.getText().toString();
            recorder.setOutputFile(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/" + fileName + ".mp3");
            dialog.dismiss();
        }
    });
}
然后我在调用stopRecording()之后添加了这个方法

现在,每当我停止录制(通过停止触摸imagebutton),应用程序就会崩溃。这是logcat文件:

08-01 22:39:26.790: E/InputEventReceiver(8258): Exception dispatching input event.
08-01 22:39:26.790: E/MessageQueue-JNI(8258): Exception in MessageQueue callback: handleReceiveCallback
08-01 22:39:26.806: E/MessageQueue-JNI(8258): java.lang.NullPointerException
08-01 22:39:26.806: E/MessageQueue-JNI(8258):   at com.whizzappseasyvoicenotepad.MainActivity.nameAlert(MainActivity.java:117)
08-01 22:39:26.806: E/MessageQueue-JNI(8258):   at com.whizzappseasyvoicenotepad.MainActivity$3.onTouch(MainActivity.java:59)
08-01 22:39:26.822: E/AndroidRuntime(8258): FATAL EXCEPTION: main
08-01 22:39:26.822: E/AndroidRuntime(8258): java.lang.NullPointerException
08-01 22:39:26.822: E/AndroidRuntime(8258):     at com.whizzappseasyvoicenotepad.MainActivity.nameAlert(MainActivity.java:117)
08-01 22:39:26.822: E/AndroidRuntime(8258):     at com.whizzappseasyvoicenotepad.MainActivity$3.onTouch(MainActivity.java:59)
它说错误在第117行和第59行

第59行:

nameAlert();
第117行:

okBtn.setOnClickListener(new OnClickListener() {
我仍然是一个初学者程序员,所以也许我用了错误的方法,我作为一个程序员的想法还不完美,所以请让我知道我如何做到这一点。谢谢大家!

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/dim"
android:padding="30dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="129dp"
    android:text="Enter the name of the recorded file:"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/etFileName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/okBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etFileName"
    android:layout_centerHorizontal="true"
    android:text="Ok" />

</RelativeLayout>

除非你真的需要为你的应用程序设计一个特定的设计,为什么不尝试使用一个简单的对话框生成器呢?您可以通过编程方式向对话框中添加编辑文本和按钮,它也可以正常工作。大概是这样的:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your message");
final EditText input = new EditText(this);
builder.setView(input);

然后只需设置正负按钮,就可以了。

显示布局文件。问题是okBtn为空,这意味着它在当前活动中找不到id为
okBtn
(如
com.whizzappseasyvoicenotepad.R.id.okBtn
)的视图。@SimonAndréForsberg编辑了我的原始帖子,请查看。问题也可能是我正在使用tablayout?在布局文件中有一个
-标记。那个真的应该在那里吗?Eclipse不是说您的XML中有一些错误吗?我看不到任何表格布局,只有RelativeLayout和RelativeLayout不应该是问题所在。@MatejJohnson:为什么您要用
com.whizzappseasyVoiceNodePad
完全限定您的资源?您应该只需要使用
R.id..
R.layout..
谢谢,我现在已经这么做了,它可以工作了。我仍然需要在将来编辑布局,所以这不会是永久性的,但出于测试目的,它很棒