-1

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>
Sebastian
  • 3,548
  • 18
  • 60
  • 95

5 Answers5

2

The problem you are facing is because of the line: 'android:parentActivityName="com.example.apple.bookshelf.StoriesActivity"'

When you are pressing the custom back button, it is creating a new instance of StoriesActivity. I'll suggest to remove:

android:parentActivityName="com.example.apple.bookshelf.StoriesActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.apple.bookshelf.StoriesActivity" />

And replace

NavUtils.navigateUpFromSameTask(this);

with

onBackPressed();
1

You have two solutions here:

First is to check if extras exists:

if(extras != null){    
    authorID = extras.getInt("author_id"); // NullPointerException here
    final String authorName = extras.getString("author_name");
    Log.i("click", Integer.toString(authorID));
}

Second solution is to use finish() instead of NavUtils.navigateUpFromSameTask(this); in StoryBodyActivity.

Hope it helps :)

0

To prevent this you can check if extra exists when coming to this activity.

 if (getIntent().hasExtra("author_id")){
        authorID = getIntent().getIntExtra("author_id",0);
    }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
0

try this way

 SharedPreferences sharedPref1 = getSharedPreferences("alldetais", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor1 = sharedPref1.edit();
                      editor1.putString("author_id", author.getID());
                editor1.putString("author_name", author.getName())
    editor1.commit();

and get this way

 SharedPreferences sharedPref1 = getApplicationContext().getSharedPreferences("alldetais", Context.MODE_PRIVATE);
        author_id=sharedPref1.getString("author_id","");
0

I didn't see your code, but I can give an advice, you have to go with the flow of the activities, visualize it as follows, Activity A -> activity B -> activity C So now, if you want to pass data using intent from A to C, then you have to send the data from A to B, then receive it in B, then again pass the received data in from B to C and then use it. Also, i think the more correct way of getting data in B from A will be Intent intent = getIntent(); intent.getExtras();