0
package com.example.projectlayout; 

import java.io.File; 

import android.media.MediaRecorder; 

import android.os.Bundle; 

import android.os.Environment; 

import android.os.SystemClock; 

import android.app.Activity; 

import android.content.Intent; 

import android.view.Menu; 

import android.view.View; 

import android.widget.Chronometer; 

import android.widget.Toast; 


public class MainActivity extends Activity { 

Chronometer time; 
int i=0; 
MediaRecorder recorder; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    time=(Chronometer)findViewById(R.id.chronometer1); 
    String name="myrecord"+i++; 
    File sdcard=Environment.getExternalStorageDirectory(); 
    System.out.println(sdcard.toString()); 
    File recording=new File(sdcard,"videoRecordingFileZ"); 
    if(!recording.exists()) 
    { 
    System.out.println("inside if"); 
    recording.mkdir(); 
    } 
String rec=recording.getAbsolutePath()+"/Record"+".3GP"; 
    try{     
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(rec);          
    recorder.prepare(); 
    } 
    catch(Exception e){ 
    System.out.println(e); 
    recorder.start(); 
    }} 
    public void startrecording(View v) 
    { 
     Toast.makeText(MainActivity.this, "START RECORDING", Toast.LENGTH_LONG).show(); 
     time.setBase(SystemClock.elapsedRealtime()); 
     time.start(); 
    } 
public void stoprecording(View v) 
    { 
     Toast.makeText(MainActivity.this, "STOP RECORDING", Toast.LENGTH_LONG).show(); 
     time.stop(); 
    } 
    public void showrecording(View v) 
    { 
     Toast.makeText(MainActivity.this, "Show RECORDING", Toast.LENGTH_LONG).show(); 
     Intent i=new Intent(MainActivity.this,Showlist.class); 
     startActivity(i); 
    } 
@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; 
}} 

的logcat:的NullPointerException在錄製音頻播放器的Android

03-27 00:49:22.224: E/AndroidRuntime(470): FATAL EXCEPTION: main 

03-27 00:49:22.224: E/AndroidRuntime(470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectlayout/com.example.projectlayout.MainActivity}: java.lang.NullPointerException 

03-27 00:49:22.224: E/AndroidRuntime(470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectlayout/com.example.projectlayout.MainActivity}: java.lang.NullPointerException 

回答

1

你沒有初始化您MediaRecorder:

MediaRecorder recorder; 

你應該這樣做在onCreate方法:

recorder = new MediaRecorder(); 

你利用你的recorder或你一個NullPointerException之前。

BTW:提高你的代碼的縮進(在Eclipse 按Ctrl + keybord快捷方式),以增加可讀性代碼


關於下一個問題:IllegalStateException

檢查MediaRecorder state diagram來自android文檔: enter image description here

和提供的示例代碼:

MediaRecorder recorder = new MediaRecorder(); 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile(PATH_NAME); 
recorder.prepare(); 
recorder.start(); // Recording is now started 
... 
recorder.stop(); 
recorder.reset(); // You can reuse the object by going back to setAudioSource() step 
recorder.release(); // Now the object cannot be reused 
+1

是的,它現在工作thnx .. – kuberkashyap

+0

好!您介意點擊此答案旁邊的接受按鈕嗎?這就是我們將問題標記爲在計算器上解決問題的方式。 – donfuxx

+0

哪裏是下一個按鈕 – kuberkashyap

1

你真的需要提供完整的錯誤消息,如果你想獲得幫助的異常調用堆棧。如上所述,我可以在你的代碼中看到一個問題(假設你已經提供了所有相關的部分)。

'recorder'成員變量從來沒有被賦值,但是在onCreate()的try和catch子句中被取消引用。這可能是導致NullPointerException的catch子句中的解引用。

+3

@ user3465656:我刪除您添加到我的答案堆棧跟蹤,因爲它是屬於你的問題,而不是在提出一個答案。還請注意,您指定用於編輯答案的原因不會顯示爲該答案下的註釋(出於某種原因)。 – cybersam

+1

我是新用戶,所以我面臨問題這個網站如何使用,,,對不起,錯誤 – kuberkashyap

+0

@ user3465656:我明白:-)。另外,您插入的堆棧跟蹤沒有顯示NullPointerException - 是整個堆棧跟蹤還是您更改了代碼?請適當修改您的問題。 – cybersam