我有一個流向MainActivity - > StoriesActivity - > StoryBodyActivity。後退按鈕會拋出NullPointerException
使用Intents將每個變量傳遞給下一個。
我試圖從StoryBodyActivity實現一個返回按鈕到StoriesActivity,但它在StoryActivity的authorID = extras.getInt("author_id");
上拋出一個NullPointerException。
我知道什麼是NullPointer。我只是不知道如何解決這個問題!難道是因爲它不再能夠訪問從MainActivity傳遞的數據嗎?
注意:單擊設備上的後退按鈕時,此功能正常工作,而不是ActionBar上的圖標。
片段從StoryBodyActivity:
從StoriesActivity@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);
}
段:
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));
}
片段從MainActivity:
從AndroidManifestIntent intent = new Intent(MainActivity.this, StoriesActivity.class);
intent.putExtra("author_id", author.getID());
intent.putExtra("author_name", author.getName());
startActivity(intent);
段:
<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>
authorID = extras.getInt(「author_id」);從這裏你發送這個值到StoriesActivity: –
@Hasmukhkachhatiya看到我的更新 – Sebastian
多數民衆贊成多數民衆贊成它的問題來自主要活動,所以回來後,你沒有持續存儲這個值的故事活動 –