I'm trying to define a button in my Home
activity to open my Settings
activity but I get the error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.padmw/com.example.padmw.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
My layout for the Settings item is in res/menu
, I tried moving it to res/layout
but then it says that element item should not be there. What should i do?
My Button in Home.class:
Button mButton = (Button) findViewById(R.id.action_settings);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Home.this, SettingsActivity.class));
}
});
My item in res/home.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>