I am trying to open a new Activity in Android Studio on a Button Press.
public class Home extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
button = findViewById(R.id.btnAddSubject);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addSubject();
}
});
}
public void addSubject(){
Intent intent = new Intent(this, AddSubject.class);
startActivity(intent);
}
}
I get the following error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.smarkmarks/com.example.smarkmarks.AddSubject}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Read more here: https://stackoverflow.com/questions/66274416/android-studio-unable-to-instantiate-activity-componentinfo
Content Attribution
This content was originally published by Herbix at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.