I have two Activities. In my main Activity I am creating an intent to start the other one:
Intent intent = new Intent(context, Settings.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and here is the code for the second Activity that should start:
public class Settings extends Activity {
Button settingsBack;
//Some other layout resources
final Context context = this; //Creating reference to this Activity Context
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
//References to layout resources
settingsBack = (Button) findViewById(R.id.settingsBack);
//...
//-----
//Assigning listeners to Buttons
settingsBack.setOnClickListener(listenerBack);
//-----
}
//Button listeners definitions
final OnClickListener listenerBack = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
//-----
}
But when this second Activity should start, I get a NullPointerException at the line, where I assign Listener to the Button. I tried to clean the project in case the R file hasnt generated properly but it didnt solve the problem. The second Activity is declared in manifest file:
<activity android:name=".Settings" android:label="@string/app_name"></activity>
Im sure its just a simple little problem somewhere but I just cant find it 🙁 Thanks for your help!
I do this all the time, I copy an activities
onCreatemethod to a new activity but forget to change the XML file in thesetContentView.I am guessing as the activity is called Settings, but the XML is called help you might have done the same!?