NullPointerException Error in android



我正在使用android API 17的媒体记录器应用程序。然而,我得到Fatal exception:mainNull Pointer Exception错误在运行时:即,同时按下记录按钮得到unfortunately stopped working错误。我做错了什么?

这是我的mainfest文件:

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="co.example.projectplay.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的。java文件

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FILE=Environment.getExternalStorageState()+"/tempRecord.3gpp";
    botn1=(Button)findViewById(R.id.botn1);
    botn1.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v)
        {
            play= MediaPlayer.create(MainActivity.this,R.raw.a);
            play.start();
            play.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer play)
                {
                    play.release();
                }
            }
            );
        }
    });

    botn2=(Button)findViewById(R.id.botn2);
    botn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            play= MediaPlayer.create(MainActivity.this,R.raw.b);
            play.start();
            play.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer play)
                {
                    play.release();
                }
            });
        }
    });
    botn3=(Button)findViewById(R.id.botn3);
    botn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            play= MediaPlayer.create(MainActivity.this,R.raw.c);
            play.start();
            play.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer play)
                {
                    play.release();
                }
            });
        }
    });
    botnRecord=(Button)findViewById(R.id.botnRecord);
    botnRecord.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            if (botnRecord.getText().toString().equals("Record"))
             {
                try 
                {
                    startRecord();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
                txtButton.setText("Recording...");
                botnRecord.setText("End");
             }
            else if(botnRecord.getText().toString().equals("End"))
             {
                stopRecord();
                txtButton.setText("");
                botnRecord.setText("Play");
             }
            else if(botnRecord.getText().toString().equals("Play"))
            {
                try {
                    startPlayback();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
                botnRecord.setText("Stop");
            }
            else
            {
                stopPlayback();
                botnRecord.setText("Record");
            }
        }   
    });
}  
public void startRecord()throws Exception
{
    if(record!=null)
    {
        //record.release();
        record.reset();
    }
    File fileOut=new File(FILE);
    if(fileOut!=null)
    {
        fileOut.delete();
    }
    record=new MediaRecorder();
    record.setAudioSource(MediaRecorder.AudioSource.MIC);
    record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    record.setOutputFile(FILE);
    record.prepare();
    record.start();
}
public void stopRecord()
{
    record.stop();
    record.release();
}
public void startPlayback()throws Exception
{
    if(play!=null)
    {
        play.stop();
        play.release();
    }
    play=new MediaPlayer();
    play.setDataSource(FILE);
    play.prepare();
    play.start();
    play.setOnCompletionListener(new OnCompletionListener()
    {
        public void onCompletion(MediaPlayer play)
        {
            play.release();
        }
    }
    );
}
public void stopPlayback()
{
    play.stop();
    play.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

这是我的日志猫错误

  • 03-11 05:13:04.803: E/AndroidRuntime(814): FATAL EXCEPTION: main
  • 03-11 05:13:04.803: E/AndroidRuntime(814): java.lang.NullPointerException
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at co.example.projectplay.MainActivity$4.onClick(MainActivity.java:106)
  • 03:11 05:13:04.803: E/AndroidRuntime(814): at android.view.View.performClick(View.java:4204)
  • 03:11 05:13:04.803: E/AndroidRuntime(814): at android.view.View$PerformClick.run(View.java:17355)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.os.Handler.handleCallback(Handler.java:725)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.os.Handler.dispatchMessage(Handler.java:92)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.os. loop .loop(loop .java:137)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.app.ActivityThread.main(ActivityThread.java:5041)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at java.lang.reflect.Method。invokeNative(本地方法)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at java.lang.reflect.Method.invoke(Method.java:511)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at com。android -11 05:13:04.803: E/AndroidRuntime(814): at dalvik.system. native . art.主要(本地方法)d.internal.os.ZygoteInit.main (ZygoteInit.java: 560)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at dalvik.system. native . art.主要(本地方法)
  • 03-11 05:13:08.062: I/Process(814):正在发送信号。Pid: 814 sig: 9

您的代码没有显示对txtButton的赋值。它可能看起来像这样:

txtButton=(TextView)findViewById(R.id.txtButton);

相关内容

  • 没有找到相关文章

最新更新