2017-04-30 13 views
0

私は私の機能send_voice.setOnClickListener(新View.OnClickListener()といくつかの問題を抱えている私はいつも同じエラーを取得:java.lang.NullPointerExceptionが:によって引き起こさ機能setOnClickListener(新しいView.OnClickListener()

public class voice extends AppCompatActivity { 

// Variables 
String name_device; 
String adress_device; 
int color=0; // color defaut : 0 colo red : 1 

private String OUTPUT_FILE; 
MediaPlayer mediaPlayer; 
MediaRecorder mediaRecorder; 



File outFile; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_voice); 

    // Image Button 
    ImageButton btn_back; 
    ImageButton btn_accueil; 
    ImageButton btn_voice; 
    ImageButton btn_scan; 
    ImageButton btn_settings; 

    final ImageButton send_voice; 

    OUTPUT_FILE= Environment.getExternalStorageDirectory()+ "/audiorecord.wav"; 
    outFile=new File (OUTPUT_FILE); 
    // Edit Text 
    EditText freq_value; 

    // Variables from other activity 
    name_device= getIntent().getStringExtra("DEVICE_NAME"); 
    adress_device=getIntent().getStringExtra("DEVICE_ADRESS"); 

    // findView by ID 
    btn_back = (ImageButton) findViewById(R.id.back); 
    btn_accueil = (ImageButton) findViewById(R.id.accueil); 
    btn_voice = (ImageButton) findViewById(R.id.voice); 
    btn_scan = (ImageButton) findViewById(R.id.scan); 
    btn_settings= (ImageButton) findViewById(R.id.settings); 

    freq_value = (EditText) findViewById(R.id.freq_value); 

    send_voice = (ImageButton) findViewById(R.id.send_voice); 



    send_voice.setOnClickListener(new View.OnClickListener() { // Bouton "send voice" 
     @Override 
     public void onClick(View view) { 

      try { 
        if (color==0) 
        { 
         send_voice.setBackgroundColor(Color.RED); 
         color=1; 
         beginRecording();      

        } 
        if (color==1) 
        { 
         send_voice.setBackgroundColor(Color.BLACK); 
         color=0; 
         stopRecording();      
        } 

       } catch (IOException e) { 
              e.printStackTrace(); 
             } 

             } 


    }); 

とXMLファイル:

ここではnullオブジェクト参照に '無効android.widget.ImageButton.setOnClickListener(android.view.View $ OnClickListener)' 私のコードを仮想メソッドを呼び出そうと

をどうやら
<ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/ic_record_voice_over_black_24dp" 
      android:layout_marginLeft="12dp" 
      android:layout_marginStart="12dp" 
      android:id="@+id/send_voice" 
      android:backgroundTint="@color/colorWhite" 
      android:layout_centerVertical="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" />` 
+2

あなたが "activity_voice" レイアウトのid "send_voice" とのImageButtonを持っていますか? – faffaffaff

+0

私の質問は@faffaffaffと同じです。答えが「いいえ」の場合は、[LayoutInflater class](https://stackoverflow.com/questions/4576330/what-does-it-mean-to-inflate-a-view-from-an)を使用することを検討する必要があります。 -xml-file)ボタン "send_voice"がレイアウト "activity_voice"内にあるべきでない場合 – horro

+0

xmlファイルを追加します。 –

答えて

0

同じ問題に直面していて、Android StudioのRクラスを生成する際に問題があります。プロジェクトをきれいにして再構築してください。それは解決されます。クリーンな再構築後

Go ->Android Studio->Build-Clean Project: 

:プロジェクトをきれいにし、再構築する

Go ->Android Studio->Build-RebuildProject: 

enter image description here

1

(スタックトレースを見て)ここでは、この:

send_voice = (ImageButton) findViewById(R.id.send_voice); 

send_voice変数のnull参照になり、あなたのImageButtonを、見つけることができません。 send_voiceが設定されていることを確認します。

関連する問題