I have a flow that goes MainActivity -> StoriesActivity -> StoryBodyActivity.
Each passes variables to the next using Intents.
I'm trying to implement a back button from StoryBodyActivity to StoriesActivity, but it's throwing a NullPointerException on authorID = extras.getInt("author_id");
in StoriesActivity.
I know what a NullPointer is. I just don't know how to fix this one! Could it be because it no longer has access to the data that was passed from the MainActivity?
Note: This works fine when clicking the back button on the device, just not the icon on the ActionBar.
Snippet from StoryBodyActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_body);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Snippet from StoriesActivity:
private int authorID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stories);
Bundle extras = getIntent().getExtras();
authorID = extras.getInt("author_id"); // NullPointerException here
final String authorName = extras.getString("author_name");
Log.i("click", Integer.toString(authorID));
}
Snippet from MainActivity:
Intent intent = new Intent(MainActivity.this, StoriesActivity.class);
intent.putExtra("author_id", author.getID());
intent.putExtra("author_name", author.getName());
startActivity(intent);
Snippet from AndroidManifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity" android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StoriesActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".StoryBodyActivity"
android:theme="@style/AppTheme"
android:parentActivityName="com.example.apple.bookshelf.StoriesActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.apple.bookshelf.StoriesActivity" />
</activity>
</application>