0

I have some problem with my function send_voice.setOnClickListener(new View.OnClickListener(). I get always the same error :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Here my code :

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();
                                         }

                                        }


    });

And xml file :

<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 Answers2

1

Apparently (when looking at the stack trace) this here:

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

cannot find your ImageButton, which results in a null reference for the send_voice variable. Make sure that the send_voice gets set.

csim
  • 610
  • 7
  • 11
0

Same problem also faced, problem in generate R class in Android Studio. better clean the project and rebuild it. it will be solve.

To clean and rebuild the project :

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

After clean rebuild.

Go ->Android Studio->Build-RebuildProject:

enter image description here

Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60