自定义DialogBox中的NPE

时间:2013-10-20 17:21:17

标签: android android-layout android-intent

我正在创建一个自定义对话框,用于录制录音机。在对话框中,有3个按钮,当我为这些按钮设置onclicklistener时,它显示强制关闭。

eveAdAudio.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mdialog = new Dialog(event_details.this);
        mdialog.setContentView(R.layout.recorder_dialog);
        final ViewHolder holder = new ViewHolder();
        mdialog.setTitle("Events...");
        holder.startRecord = (Button) mdialog.findViewById(R.id.startRecordBtn);
        holder.stopRecord = (Button) mdialog.findViewById(R.id.stopRecordBtn);
        holder.saveRecord = (Button) mdialog.findViewById(R.id.saveRecordbtn);
        holder.showRecord = (TextView) mdialog.findViewById(R.id.recordShowText);
        holder.nameRecord = (EditText) mdialog.findViewById(R.id.editAudioName);

        holder.nameRecord.setText("myAudioFile");

        holder.startRecord.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                p=1;
                holder.startRecord.setEnabled(false);
                holder.stopRecord.setEnabled(true);
                holder.showRecord.setVisibility(View.VISIBLE);
                anim = new AlphaAnimation(0.0f, 1.0f);
                anim.setDuration(800); //You can manage the time of the blink with this parameter
                anim.setStartOffset(20);
                anim.setRepeatMode(Animation.REVERSE);
                anim.setRepeatCount(Animation.INFINITE);
                holder.showRecord.startAnimation(anim);
                try {
                    startRecording();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
        holder.stopRecord.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                p=2;
                holder.startRecord.setEnabled(true);
                holder.saveRecord.setEnabled(true);
                holder.stopRecord.setEnabled(false);
                holder.showRecord.clearAnimation();
                holder.showRecord.setVisibility(View.INVISIBLE);
                try {
                    //stopRecording();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                holder.nameRecord.setEnabled(true);
            }
        });
        holder.saveRecord.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                audioFileName = holder.nameRecord.getText().toString()+".mp4";
                p=3;
                mdialog.dismiss();

            }
        });
        mdialog.show();

    }
});

这是观察者类

private class ViewHolder{

    Button startRecord; 
    Button stopRecord; 
    Button saveRecord; 
    TextView showRecord;
    EditText nameRecord;
}

starRecording和stopRecording函数ae给出如下。

public void startRecording() throws IOException{

        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        audiofile =getFilename(audFolder);//path of file;
        recorder.setOutputFile(audiofile);
        recorder.setOnErrorListener(errorListener);
        recorder.setOnInfoListener(infoListener);
        try {
            recorder.prepare();
            recorder.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopRecording() throws Exception
    {
        if(null != recorder){
            recorder.stop();
            recorder.reset();
            recorder.release();
            recorder = null;
    }
    }

自定义对话框的布局如下:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:padding="10dp"
    >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/voice" />
        <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:gravity="center"
    android:layout_gravity="center"
    >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="      AuFriDis\nVoice Recorder" 
            android:textSize="15dp"
            android:typeface="serif"
            android:textStyle="bold|italic"
            android:layout_marginRight="15dp"/>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:gravity="center"
    android:layout_gravity="center"
    >



        <TextView
        android:id="@+id/recordShowText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Recording..."
        android:textColor="#ffffff"
        android:textStyle="bold|italic"
        android:textSize="15dp" 
        android:visibility="invisible"/>

        <EditText
            android:id="@+id/editAudioName"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:ems="10" 
            android:visibility="invisible">
        </EditText>

        </LinearLayout>
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_marginTop="10dp"
    android:gravity="center">

        <Button
              android:id="@+id/startRecordBtn"
              android:layout_width="70dp"
              android:layout_height="40dp"
              android:textColor="#ff0000"
              android:background="@drawable/item_background"
              android:text="Start" 
              android:layout_marginRight="4dp"/>

         <Button
              android:id="@+id/stopRecordBtn"
              android:layout_width="70dp"
              android:layout_height="40dp"
              android:textColor="#0066ff"
              android:background="@drawable/item_background"
              android:text="Stop" 
              android:layout_marginRight="4dp"
              android:enabled="false"/>

          <Button
              android:id="@+id/saveRecordbtn"
              android:layout_width="70dp"
              android:layout_height="40dp"
              android:textColor="#ffffff"
              android:background="@drawable/item_background"
              android:text="Save" 
              android:enabled="false"/>


     </LinearLayout>
</LinearLayout>

错误行如下:

10-20 22:23:39.155: E/AndroidRuntime(1528): FATAL EXCEPTION: main
10-20 22:23:39.155: E/AndroidRuntime(1528): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.view.ViewRoot.setView(ViewRoot.java:509)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.app.Dialog.show(Dialog.java:241)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at MainPRJ.Diary.event_details$6.onClick(event_details.java:353)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.view.View.performClick(View.java:2408)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.view.View$PerformClick.run(View.java:8816)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.os.Handler.handleCallback(Handler.java:587)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.os.Looper.loop(Looper.java:123)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at java.lang.reflect.Method.invoke(Method.java:521)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-20 22:23:39.155: E/AndroidRuntime(1528):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

要显示的对话框为空或可能未初始化。

AlertDialog mAlertDialog = new AlertDialog.Builder(this)
.setView(R.layout.your_custom_layout_view_for_dialog)
.create();

通过这种方式我觉得更容易。