0

I have a little problem: I have 3 Activity. Activity ListClass.java

public class ListClass extends Activity { ...... listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { onItemClick1(parent, view, position, id); } }); } public void onItemClick1(AdapterView<?> parent, View view, int position, long id) { String name = (String) parent.getItemAtPosition(position); String address = (String) listAddress.get(position); int status = (int) listStatus.get(position); double lat = (double) listLat.get(position); double lng = (double) listLng.get(position); String serialNumber = (String) listSerialNumber.get(position); getIntent().putExtra("name", name); getIntent().putExtra("address", address); getIntent().putExtra("status", status); getIntent().putExtra("latitude", lat); getIntent().putExtra("longitude", lng); getIntent().putExtra("serialNumber", serialNumber); startActivity(getIntent()); } public Intent getIntent() { Intent intent = new Intent(ListClass.this, ClassB.class); return intent; } 

As you can see I putExtra values that I use on ClassB.java :

public class ClassB extends Activity { TextView textViewTitle; TextView textView2; TextView textViewInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.charge_box); this.textViewTitle = (TextView) findViewById(R.id.charge_box__textView1); this.textView2 = (TextView) findViewById(R.id.charge_box__textView2); this.textViewInfo = (TextView) findViewById(R.id.charge_box__textView3); getActionBar().setDisplayHomeAsUpEnabled(true); Bundle bundle = getIntent().getExtras(); String name = bundle.getString("name"); String address = bundle.getString("address"); int status = bundle.getInt("status"); textViewTitle.setText(name); textViewInfo.setText(address + " " + status); } } 

SO FAR SO GOOD! But I need to start ActivityB from another activity that is MainClass.java.

 Intent intent = new Intent(MainActivity.this, ClassB.class); startActivity(intent); 

the problem is that when ClassB uses Bundle bundle = getIntent().getExtras(); he doesn't find "extras" because Intent of MainClass is different from Intent of ListClass! how can I do?? can I decide which Intent use? tahnk you!

EDIT: @skygeek ok I'm working on it, but how can I create a static variable if the extra values change?

public void onItemClick1(AdapterView<?> parent, View view, int position, long id) { String name = (String) parent.getItemAtPosition(position); String address = (String) listAddress.get(position); int status = (int) listStatus.get(position); double lat = (double) listLat.get(position); double lng = (double) listLng.get(position); String serialNumber = (String) listSerialNumber.get(position); Intent intent = new Intent(ListClass.this, ChargeBoxInfoActivity.class); intent.putExtra("name", name); intent.putExtra("address", address); intent.putExtra("status", status); intent.putExtra("latitude", lat); intent.putExtra("longitude", lng); intent.putExtra("serialNumber", serialNumber); startActivity(intent); } 
0

4 Answers 4

2

Make a check inside ClassB's onCreate method

 if(getIntent().getExtras() != null) { // do your functionality with extras Bundle bundle = getIntent().getExtras(); String name = bundle.getString("name"); String address = bundle.getString("address"); int status = bundle.getInt("status"); textViewTitle.setText(name); textViewInfo.setText(address + " " + status); } else { // do whatever you want to do } 
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, Thaknk you for answering! I tried this solution but the problem is that whether the bundle is null or not, I want to use the intent of ListCLass. So i don't know what to do in "else case". any idea?
but how can you use the listclass intent until you send it.
I don't know. I thought that when you call getIntent() method, it get the Intent of the Activity that launch ClassB. So if I start ClassB from ListClass I have no problem but if I start from MainClass the app crash. I'm a bit confused...
your app is crashing because you are sending some values inside extras when starting from list class but when you start the classB from mainactivity you are not sending any extras thats why you need to put the above check and write the code which you are using for getting extras inside if block
ok, I got it. But if getIntent().getExtras == null (so the second chek else), how can I call the extras from ListClass. Extra values are only in ListClass because are part of ArrayLists.sorry if I don't understand but I'm a beginner.
|
0

create an static object with static field intent when calling the activity for first time(check if its null). So next time you call the same activity from another class use this same static object having the already set intent to call the activity again and hence you will find those extras!

3 Comments

tahks for your answer :) Actually I tried this solution but Eclipse said "null poiter exception". could you please show me an example? thanks again!
@Pikkio try posting the code that you have tried as per my suggestion and i'll definitely try to help you!!!!
dont use static objects to call an activity as this results to potential memory leaks. check an article about avoiding memory leaks
0

This is wrong:

getIntent().putExtra("name", name); getIntent().putExtra("address", address); getIntent().putExtra("status", status); getIntent().putExtra("latitude", lat); getIntent().putExtra("longitude", lng); getIntent().putExtra("serialNumber", serialNumber); startActivity(getIntent()); 

}

public Intent getIntent() { Intent intent = new Intent(ListClass.this, ClassB.class); return intent; 

}

You create new intent after each function call getIntent()... Try this:

Intent intent = new Intent(ListClass.this, ClassB.class); intent.putExtra("name", name); intent.putExtra("address", address); intent.putExtra("status", status); intent.putExtra("latitude", lat); intent.putExtra("longitude", lng); intent.putExtra("serialNumber", serialNumber); startActivity(intent); 

1 Comment

ok! I did it, but the problem still persist. when I start ClassB from ListCLass there are no problems but if I try to start from MainActivity the app crash. the problem is taht Intent of mainClass has not extra values.
0

I understand... try this:

try{ Bundle bundle = getIntent().getExtras(); String name = bundle.getString("name"); String address = bundle.getString("address"); int status = bundle.getInt("status"); textViewTitle.setText(name); textViewInfo.setText(address + " " + status); }}catch (Exception ignored) { } 

1 Comment

it doesn't work. I'm really afraid I will not solve this prob.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.