0

I have two activities, Activity A, and B. When user clicks on a topicTextView in the Activity A, it takes him to Activity B where user choses a Topic and pass selectedTopic to the Activity A.

I have implemented the following but app crashes first time when opens Activity A.

Activity A

@Override
protected void onCreate(Bundle savedInstanceState) {

  topicTextView = (TextView) findViewById(R.id.topicTextView);
  // crashes in the following line
  String selectedTopic = getIntent().getExtras().getString("selectedTopic");
  if (selectedTopic != null && !selectedTopic.isEmpty()) {
   topicTextView.setText(selectedTopic);
  }
}

Activity B

@Override
 protected void onCreate(Bundle savedInstanceState) {
   listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)   {
      Intent i = new Intent(ActivityB.this, ActivityA.class);
      i.putExtra("selectedTopic", topics[position].toString());
      startActivity(i);
   }
 });
}
casillas
  • 16,351
  • 19
  • 115
  • 215

3 Answers3

2

before fetching value , check weather Bundle is having that value or not. Problem in your case is when you run for first time, getIntent().getExtras().getString("selectedTopic"); will return null

if(getIntent().hasExtra("selectedTopic"))
{
    String selectedTopic = getIntent().getExtras().getString("selectedTopic");
    if (selectedTopic != null && !selectedTopic.isEmpty()) {
      topicTextView.setText(selectedTopic);
    }
}

Another good way is to use startActivityForResult() and pass your value from activity B which you will get in onActivityResult() of activity A, check this reference question for this approach

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • I will mark your answer when sof allows me, I have upvoted. I have tested and works like a charm. Could you please also illustrate the other approach just in case as a reference if you don't mind. – casillas Feb 10 '16 at 06:24
  • @texas yes i have given link in updated answer, refer that for second approach – Ravi Feb 10 '16 at 06:26
1

It is coming null because the very first time you are coming to activity A, you do not have that string in the intent so put a check whether that string is there in the intent

if(getIntent().hasExtra("selectedTopic")){
String selectedTopic = getIntent().getExtras().getString("selectedTopic");
  if (selectedTopic != null && !selectedTopic.isEmpty()) {
   topicTextView.setText(selectedTopic);
  }
}

Better is to use startActivityForResult() to achieve this

Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53
0

Change your code:

String selectedTopic = null;
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("selectedTopic")){
 selectedTopic  = getIntent().getExtras().getString("selectedTopic");
}

if (selectedTopic != null && !selectedTopic.isEmpty()) {
   topicTextView.setText(selectedTopic);
}
Rakesh
  • 756
  • 1
  • 9
  • 19