I have 3 activities we will call them A, B and C. A and B both have intents that send the view to C. And design on which one C came from different things happen on C's activity. I pass the data like this from A to C:
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
startActivity(intent);
Then in C's onCreate method I have something like this:
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Then I from B to C I have
Intent intent = new Intent(this, C.class);
startActivity(intent);
And then I get the NullPointerException, I am guessing this is because i have not specifed a String "Action" when going from B to C.
Now I could just add one line for B in this case but, if there were for more activities or a large project this would not be good, because for each activity going to one I would need this.
How can I have it so I don't get this exception without adding an "Action" sting to activity B?
Thanks for the help in advance.
EDIT
If I have this from A to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
intent.putExtra("Action2", "A");
startActivity(intent);
And this from B to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "B");
startActivity(intent);
And then this in onCreate of C it fails when going from B to C:
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
else if (extras.getString("Action2").equals("A")) {
//DO Stuuf
}
}
Change the code in C class like this for checking the bundle is null or not.
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getString("Action") != null)
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
}
if(extras.getString("Action2") != null)
if (extras.getString("Action2").equals("A2")) {
//DO SOMETHING
}
}
}
check below code :
if(getIntent().hasExtra("Action"))
//if you have passed "Action" from activity
else
//if you did not pass "Action" from activity
Try this...
Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}
Hope this solves
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}
you are always creating new Intent object when ever you are moving to new activity. Hence this issue. Try to copy required data in new intent and access, them.
NullPointerException This will Happen because When you are moving from Activity A to C you are passing intent with value and in activity C you are getting that value using Bundle. But When you Move From Activity B to C you are not passing values through intent and in Activity C you have Written this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
at this tme the extra is null So you will get Error.
DO like this
Bundle extras = getIntent().getExtras();
if(extras != null)//This one will check for null
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
EDIT
For passing Multiple values from activity
Intent intent= new Intent(this,NextActivity.class);
Bundle extra = new Bundle();
extra.putString("Action1","Value1");
extra.putString("Action2","Value2");
intent.putExtras(extra);
startActivity(intent);
In NextActivity
Intent i = getIntent();
Bundle extras = i.getExtras();
String strAction1 = extras.getString("Action1");
String strAction2 = extras.getString("Action2");
You are starting the Activity C twice( whenever you call startActivity(intent) ). First time from A and second time from B. Both times you are checking this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Which is bound to give you a null pointer exception. Put up a break point in your IDE and verify it.
why don't you use some static variable to share the info between the activities in case you don't want to instantiate the class C more than once.
Related
I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;
Currently I have an ArrayList that I wanted to pass to another activity. I've already applied getParcelableArrayListbut it pass null to another activity. I've check the array for null, it's not. In the current activity before I pass it at least.
This is my code:
RecyclerViewOffline.java
Toast.makeText(context,"Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(),OfflineActivity.class);
Log.d("Contexts Test : ", fileContents.get(holder.getLayoutPosition()).getTitle());
Bundle bundle = new Bundle();
//bundle.putParcelableArrayList("link",fileContents);
bundle.putParcelableArrayList("ArrayList",fileContents);
bundle.putInt("position",holder.getLayoutPosition());
intent.putExtra("bundle",bundle);
view.getContext().startActivity(intent);
My receiving end:
OfflineActivity.java
Bundle extras = getIntent().getBundleExtra("bundle");
if(extras != null) {
this.fileContents = extras.getParcelableArrayList("ArrayList");
layoutPosition = extras.getInt("position");
Log.d("Contexts Test Offline: ",""+ fileContents.get(layoutPosition).getTitle());
}
else{}
As I can see
You didnt add the arraylist in the Parcelable in and out method and contructor.
This will solve your issue.
I am attempting to pass an int through an intent to another class and have managed to successfully pass through the integer however I am unsure as how to convert a Bundle to an Integer.
Code from Intent:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class).putExtra("totalTime", totalTime);
startActivity(intent);
}
Code in Timer class:
Bundle time = getIntent().getExtras();
if(time == null)
{
timeDisp.setText("Failed.");
}
else
{
totalTimeMs = Integer.parseInt(String.valueOf(time));
timeDisp.setText(totalTimeMs);
}
Thanks in advance :)
Intent can hold directly all java primitives types and parcelable/serializable objects.
You may have a confusion that it can also hold Bundles.
Do you really need to put your integer in a Bundle? It can be true for multiple values that logically coupled.
Check Intent API.
If your totalTime is of type int which you pass through putExtra() the you can use:
int time = getIntent().getExtras().getInt("totalTime");
You are not adding your intent to a Bundle, so on the recieving activity you are trying to get data out of an empty Bundle.
You would Add the data to a bundle by:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class);
Bundle b = new Bundle():
b.putString("totalTime", totaltime);
intent.putExtras(b);
startActivity(intent);
}
Then you would retrieve the String from the bundle:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String time = extras.getString("totalTime");
how to redirect from Normal java class to other java class which extends activity in android with putting some data.
to start a Another activity class
Intent i = new Intent(this, ActivityTwo.class);//this your current class
startActivity(i);
to pass a value in class 1
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
to get values .from ActivityTwo.class
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// do something with the data
}
I'm trying to pass a string between two activities. I've done this in other projects using the same method, but for some reason I'm getting a NullPointerException when I call intent.getStringExtra(String). I have also tried creating a Bundle for the extras via
Bundle b = getIntent().getExtras();
but that also returned null. Below is the code that I am currently trying to use.
Activity A:
Intent myIntent = null;
String select = "";
if (selection.equals("Chandelle")) {
myIntent = new Intent(Commercial.this, Chandelle.class);
select = "Chandelle";
} else if (selection.equals("Eights on Pylons")) {
myIntent = new Intent(Commercial.this, EightsOnPylons.class);
select = "Eights on Pylons";
}
// Start the activity
if (myIntent != null) {
myIntent.putExtra("selection", select);
Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
startActivity(myIntent);
}
Here's the code in activity B that tries to pull the extra:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
I've tried pretty much every alternative way of passing extras, but they all seem to behave this way. What am I missing?
Add .ToString() to myIntent.putExtra("selection", select); so that it is myIntent.putExtra("selection", select.ToString());
Luckily I found this post. I've been struggling with this for hours and in the end I realized that I'm sending my intent to the tabHost as well. ;-) For those who are still having problems with this just get the extras from the tabHost activity itself and pass it along to the child tabs.
String userID;
.
.
Bundle getuserID = getIntent().getExtras();
if (getuserID != null) {
userID = getuserID.getString("userID");
}
...
intent = new Intent().setClass(this, Games.class);
intent.putExtra("userID", userID);
spec = tabHost.newTabSpec("games").setIndicator("Games",
res.getDrawable(R.drawable.tab_games))
.setContent(intent);
tabHost.addTab(spec);
Try the code below. I have added an override to your code, which will do the trick:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
}
Look at your code, I think it may happen when you did put anything in Activity 1. Because you use "if...else if", and you don't have "else" for other cases. If that happens then you will get null value in the Activity 2. Check again this case.
If you still have not put anything in the bundle, it will always return null!
You can create and set the bundle by your own :
Intent i = new Intent( ... );
i.putExtras(new Bundle());
Bundle bundle= i.getExtras(); // (not null anymore)
Or, put a dummy value just to force the initialization (I prefer the first solution):
Intent i = new Intent( ... );
i.putExtra("dummy", true);
Bundle bundle= i.getExtras(); // (not null anymore)
My problem was resolved when I figured out not to startAcitvity() before I used putExtra().
Newbie mistake but I hope somebody will find it useful.
I changed this
Intent i1 = new Intent(MainActivity.this,Second.class);
startActivity(i1);
String abc = e1.getText().toString();
i1.putExtra("user",abc);
to this
Intent i1 = new Intent(MainActivity.this,Second.class);
String abc = e1.getText().toString();
i1.putExtra("user",abc);
startActivity(i1);
In activity B you at the following function:
protected String getStringExtra(Bundle savedInstanceState, String id) {
String l;
l = (savedInstanceState == null) ? null : (String) savedInstanceState
.getSerializable(id);
if (l == null) {
Bundle extras = getIntent().getExtras();
l = extras != null ? extras.getString(id) : null;
}
return l;
}
You are using it in the following way in activity B:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String MANEUVER_ID = getStringExtra(savedInstanceState, "selection");
}