java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference #52
I tried using the example code at the bottom of the page to test it out. But I'm getting this error whenever I put the java code in. Otherwise the XML part works fine. But the minute I put the java code the function setOnClickedButtonListener and OnPositionChangedListener just causes the app to crash.
Main Activity
package com.masslabs.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import co.ceryle.radiorealbutton.RadioRealButton;
import co.ceryle.radiorealbutton.RadioRealButtonGroup;
public class MainActivity extends AppCompatActivity {
final RadioRealButton button1 = (RadioRealButton) findViewById(R.id.button1);
final RadioRealButton button2 = (RadioRealButton) findViewById(R.id.button2);
RadioRealButtonGroup group = (RadioRealButtonGroup) findViewById(R.id.group);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// onClickButton listener detects any click performed on buttons by touch
group.setOnClickedButtonListener(new RadioRealButtonGroup.OnClickedButtonListener() {
@Override
public void onClickedButton(RadioRealButton button, int position) {
Toast.makeText(MainActivity.this, "Clicked! Butten: " + button.getText(), Toast.LENGTH_SHORT).show();
}
});
// onPositionChanged listener detects if there is any change in position
group.setOnPositionChangedListener(new RadioRealButtonGroup.OnPositionChangedListener() {
@Override
public void onPositionChanged(RadioRealButton button, int currentPosition, int lastPosition) {
Toast.makeText(MainActivity.this, "Button Changed! Text: " + button.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
I tried using the example code at the bottom of the page to test it out. But I'm getting this error whenever I put the java code in. Otherwise the XML part works fine. But the minute I put the java code the function setOnClickedButtonListener and OnPositionChangedListener just causes the app to crash.
Main Activity
XML File