1

This is a basic activity swapping.

The app does not crash if i declare a local button inside the configureActivitySwap() method like this:

Button voiceBtn = (findViewById(R.id.goToVoice));

But I have to declare the button in the global scope instead so I can use the button in other methods, mainly activating and deactivating the button when it should/should not be pressed.

I also noticed that if I remove the finish(); method and replace it with something else the app functions normally, but I have to have the finish(); method one way or another.

public class RecogActivity extends AppCompatActivity {

    private Button voiceBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        voiceBtn = findViewById(R.id.goToVoice);
        setContentView(R.layout.main_layout);

        // some unrelated code

        configureActivitySwap();
    }

    public void configureActivitySwap(){
        // Button voiceBtn = (findViewById(R.id.goToVoice));

        voiceBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}

My runtime error logs:

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: tk.gandriks.gaaudiotransform, PID: 23125
        java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.gandriks.gaaudiotransform/tk.gandriks.gaaudiotransform.RecogActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
            at android.os.Handler.dispatchMessage(Handler.java:105)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6944)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at tk.gandriks.gaaudiotransform.RecogActivity.configureActivitySwap(RecogActivity.java:140)
            at tk.gandriks.gaaudiotransform.RecogActivity.onCreate(RecogActivity.java:124)
            at android.app.Activity.performCreate(Activity.java:7183)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
            at android.app.ActivityThread.-wrap11(Unknown Source:0) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
            at android.os.Handler.dispatchMessage(Handler.java:105) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6944) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
Greg
  • 357
  • 1
  • 11

4 Answers4

4

You need to call the setContentView() before calling voiceBtn = findViewById(R.id.goToVoice); Since you don't specify the layout the findViewById method will not get the button instance

public class RecogActivity extends AppCompatActivity {

   private Button voiceBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      // set the layout first
      setContentView(R.layout.YOUR_LAYOUT_XML_FILE_NAME)

      voiceBtn = findViewById(R.id.goToVoice);

      // some unrelated code

      configureActivitySwap();
 }

 public void configureActivitySwap(){
    // Button voiceBtn = (findViewById(R.id.goToVoice));

    voiceBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        finish();
    }
  });
}
sanoJ
  • 2,935
  • 2
  • 8
  • 18
  • Pardon me, i made this thread in a rush and i accidentally mistook the `setContentView();` method for unrelated code and didn't mention it. I am already using it, this is not the issue. Here, i edited the code as it currently is, and the issue persists. – Greg May 29 '19 at 11:01
  • @Greg You must call `setContentView` before `voiceBtn = findViewById(R.id.goToVoice);` – sanoJ May 29 '19 at 11:03
  • Oh damn... I think i may be an idiot – Greg May 29 '19 at 11:05
1

Try I guess) In your // some unrelated code is contains setContentView method?

public class RecogActivity extends AppCompatActivity {

    private Button voiceBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        voiceBtn = findViewById(R.id.goToVoice);

        setContentView(R.layout.some_layout)
        // some unrelated code

        configureActivitySwap();
    }

    public void configureActivitySwap(){
        // Button voiceBtn = (findViewById(R.id.goToVoice));

        voiceBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}

You caught NPE because of findViewById is calling on inflated view. You are have been calling findViewById before setContentView in the first case and got the exception. And in the second case - in configureActivitySwap, that going after setContentView. Move setContentView after super.onCreate(savedInstanceState) and all will be working fine.

Scrobot
  • 1,911
  • 3
  • 19
  • 36
1

Are you setting layout before trying to find view with findViewById? setContentView(R.layout.main_layout);

Tomislav29
  • 78
  • 1
  • 6
-2
voiceBtn = (Button) findViewById(R.id.goToVoice);

replace the statement in your onCreate() method with the above. It should work.

and use

super.finish() instead of finish()
Sajith
  • 713
  • 6
  • 21
Mian Faizan
  • 23
  • 1
  • 7