2015-12-03 62 views
0

我想讓自己成爲一個簡單的rec按鈕來捕獲用戶的音頻。 我找到指南here,我試圖改變一些東西,但發生錯誤,我不知道是什麼問題。Android簡單語音捕獲按鈕,NullPointerException

logcat的錯誤:

12-03 18:43:21.773 11093-11093/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ddz.diarioscolastico/com.ddz.diarioscolastico.ActivityRegistrazione}: java.lang.NullPointerException 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
     at android.app.ActivityThread.access$700(ActivityThread.java:140) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4921) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
     at com.ddz.diarioscolastico.ActivityRegistrazione.onCreate(ActivityRegistrazione.java:114) 
     at android.app.Activity.performCreate(Activity.java:5206) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
     at 

而且有我改變了代碼

public class ActivityRegistrazione extends Activity { 

    private static final String LOG_TAG = "AudioRecordTest"; 
    private static String mFileName = null; 

    //private RecordButton mRecordButton = null; 
    private MediaRecorder mRecorder = null; 

    //private PlayButton mPlayButton = null; 
    private MediaPlayer mPlayer = null; 

    private void onRecord(boolean start) { 
     if (start) { 
      startRecording(); 
     } else { 
      stopRecording(); 
     } 
    } 

    private void startRecording() { 
     mRecorder = new MediaRecorder(); 
     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mRecorder.setOutputFile(mFileName); 
     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

     try { 
      mRecorder.prepare(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "prepare() failed"); 
     } 

     mRecorder.start(); 
    } 

    private void stopRecording() { 
     mRecorder.stop(); 
     mRecorder.release(); 
     mRecorder = null; 
    } 

    /*class RecordButton extends Button { 
     boolean mStartRecording = true; 

     OnClickListener clicker = new OnClickListener() { 
      public void onClick(View v) { 
       onRecord(mStartRecording); 
       if (mStartRecording) { 
        setText("Stop recording"); 
       } else { 
        setText("Start recording"); 
       } 
       mStartRecording = !mStartRecording; 
      } 
     }; 

     public RecordButton(Context ctx) { 
      super(ctx); 
      setText("Start recording"); 
      setOnClickListener(clicker); 
     } 
    }*/ 

    public ActivityRegistrazione() { 
     //mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     File mFolder = new File(Environment.getExternalStorageDirectory() + "/diarioscolastico"); 
     mFileName = Environment.getExternalStorageDirectory() + "/diarioscolastico"; 


     boolean success = true; 
     if (!mFolder.exists()) { 
      success = mFolder.mkdir(); 
     } 
     if (success) { 

      // Create an audio file name 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
      String audioFileName = "AUDIO_" + timeStamp + "_.3gp"; 
      // Do something on success 
      mFileName += "/"+ audioFileName ; 

     } else { 
      // Do something else on failure 
     } 

    } 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     final Button recButton = (Button) findViewById(R.id.recButton); 
     recButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       boolean mStartRecording = true; 

       onRecord(mStartRecording); 
       if (mStartRecording) { 
        recButton.setText("Stop recording"); 
       } else { 
        recButton.setText("Start recording"); 
       } 
       mStartRecording = !mStartRecording; 
      } 
     }); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mRecorder != null) { 
      mRecorder.release(); 
      mRecorder = null; 
     } 

     if (mPlayer != null) { 
      mPlayer.release(); 
      mPlayer = null; 
     } 
    } 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_activity_registrazione, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

的問題是,例如使用從Java創建一個按鈕,但我需要XML格式的靜態按鈕,這樣我嘗試生成它並使用id進行搜索,但是當我啓動應用程序崩潰的活動並且錯誤出現在logcat中時。

有些想法?

+1

你能告訴我這是什麼嗎? ActivityRegistrazione.java line 114 因爲根據錯誤日誌,它發生在那裏。併發布你的XML文件 – user3162662

回答

0

我認爲你錯過了onCreate處理程序的一行。

調用super.onCreate(...),並尋求意見之前後,你應該設置的內容,這樣的事情:

super.onCreate(icicle); 
setContentView(R.layout.mylayout); 
final Button recButton = (Button) findViewById(R.id.recButton); 

其中mylayout與此觀點,幷包含相關佈局名稱recButton。