Thanks in advance.
When I print Log.d("me",getIntent().toString());
I am getting:
Intent { act=android.intent.action.CALL_PRIVILEGED dat=tel:888 flg=0x13800000 cmp=com.ninetology.freecall.two/.CallFinalActivity }
I am trying to fetch the value which is associated with "dat" but I am getting NullPointer exception.
//the code I am using is
getIntent().getStringExtra("dat"); // no use
//i tried
getIntent().getExtras("dat").toString(); // NullPointer exception
I tried with "tel" as key in above code still no use.
it seems you're doing this wrong.
The getExtras() function returns a bundle that you can extract data from and not a function that returns a specific String.
dat is NOT a String value as you can see from the data that was printed. it's a Uri,
try parsing it as you should and I'm sure you'll be able to get the data.
public void onCreate(Bundle b) { //mistyped
super.onCreate(b);
Uri data = getIntent().getData();
// OR USE THIS
String data = getIntent().getDataString();
// DO STUFF
}
try getIntent().getExtras().get("dat");
First of all, Its not necessary the string from Intent your are getting in log have a object with values..
So its better to just check its not a null, like,
Bundle bundle = getIntent().getExtras();
if(bundle ! = null)
{
// Now check you bundle object which has a values or not
}
else
{
// 1. get data in form of Uri
Uri data = getIntent().getData();
// 2. OR get string of Uri
String dataString = getIntent().getDataString();
// 3. Or split the data string
// The logic from this part may be different on your requirement.. I only suggests you to get data from string.. (Actual logic may different on your case)
String data = getIntent().toString();
data = data.subString(data.indexOf(":"), data.indexOf("flg")-1);
Log.e("tel:", data);
}
When you want to pass the data with the intent just add the below code before starting activity
intent.putExtra("dat", value); //value=the value you want to send
And when you want to fetch the same value in another activity just do:
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String string=bundle.getString("dat");
}
By doing this, you wont get the null pointer exception and will help you.
Related
Hitting a brick wall in my code at the moment for fetching json objects from multiple pages(using a loop) in a AsyncTask. It reaches the last page, but getting the correct if statement to ensure that the loop DOESN'T run again and continues on is baffling me.
String data = //some correct json data with next element that holds a uri parseable string
JSONObject initial = new JSONObject(data);
String next = initial.getString(nextObjSTR);
//gonna start from the "last" page and recursively return to the 1st page
if(*The if condition I need help with*) {
//there is another page
makeConnection(Uri.parse(next));
}
Basically, the last page of json elements has a next element with a null or no element value, which triggers the IOException error caught in makeConnection method because my initial if statement has always been failing.
Can I get a reason or help as to the appropriate if check for Strings from json? I've tried String != null as NullPointerExceptions occur if I use any method from String to compare. Likewise, JsonObject.NULL comparison doesn't work for me either.
None of the other answers worked, and I ended up questioning whether the element was really null despite looking at the parsed json data via an online tool. In the end, JSONObject.IsNull(element mapping name) is the right approach.
If you're sure that the value is either null (empty) or a correct URI, and assuming that the nextObjSTR key is always present in the data JSON, then that will do:
if (next != null && !next.trim().isEmpty()) {
makeConnection(Uri.parse(next));
}
Or, since you're on Android, it's better use the more convenient method:
if (!TextUtils.isEmpty(next)) {
makeConnection(Uri.parse(next));
}
You can use the optString Method of the JSONObject. If the JSON key is not this method will return a empty string, so you can check it easily:
String next = initial.optString(nextObjSTR);
if ( ! next.isEmpty() ) {
makeConnection(Uri.parse(next));
}
Source: https://developer.android.com/reference/org/json/JSONObject.html#optString(java.lang.String)
you must check value with key is has in json object.
Try below code:
JSONObject initial = new JSONObject(data);
if(initial.has(nextObjSTR)) {
String next = initial.getString(nextObjSTR);
if (next != null && !next.isEmpty()) {
makeConnection(Uri.parse(next));
}
}
I do like this...
String value;
if(jsonObject.get("name").toString.equals("null")){
value = "";
else{
value = jsonObject.getString("name");
}
I use intents to communicate from a service to the activity. I use intent.putExtra(Tag, message); if I have a error or something.
In the Activity I can get these Extras like following
String message_error_1 = intent.getStringExtra("Message_Error_1");
String message_error_2 = intent.getStringExtra("Message_Error_2");
String message_error_3 = intent.getStringExtra("Message_Error_3");
but just one has data. I can now make an If statement for every entry but I think there is a way to figere out which entry has data. Is there a way?
If there's only going to be one error, I'd consider passing an ERROR_TYPE int as one extra and the ERROR_MESSAGE string as another extra, so you don't have to write so many if statements. For example:
int type = intent.getIntExtra("error_type"); // 1, 2, or 3
String message = intent.getStringExtra("error_message");
To answer your question, you could have the service put in an extra that indicates which error message key to use. For example:
// In Service
intent.putExtra("error_key", "Message_Error_1"); // error 1 has the message!
// In Activity
String key = intent.getStringExtra("error_key");
String actualErrorMessage = intent.getStringExtra(key);
I wish to pass the value of score from one activity to another. I addExtras to the intent and getExtras in the new activity but it doesn't seem to get the value.
Activity 1;
Intent intent = new Intent(Game.this, EndGame.class);
intent.putExtra("put_score", score);
startActivity(intent);
Game.this.finish();
Activity 2;
Bundle extras = getIntent().getExtras();
if (extras != null) {
score = extras.getString("put_score");
}
setContentView(R.layout.endgame);
scoreResult = (TextView) findViewById(R.id.scoreNum);
scoreResult.setText(score);
You problem is coming from the following piece of code in Bundle.java:
try {
return (String) o;
} catch (ClassCastException e) {
typeWarning(key, o, "String", e);
return null;
}
Here o is the object you put to the bundle (bundle actually has a core storage of type Map<String, Object>, so, due to autoboxing, when you put int to the bundle, it will become Integer). But, unfortunately, Integer cannot be casted to String explicitly, so you get null instead. In other words: if you put int, then use getInt to retrieve the value.
you placed data in intent using putExtra not putExtras
so read them the same way
use
getXXExtra()
XX is the dataType your data is,
based on the example, if score is Integer, then use:
getIntExtra("put_score", 0);//0 zero is default in case data was not found
http://developer.android.com/reference/android/content/Intent.html
Am passing a string from on activity to another, its working but if i try and open the activity without passing the strings It throws a Null pointer exception, Kindly Assit
Bundle gotBasket = getIntent().getExtras();
gotPassenger= gotBasket.getString("passenger");
gotStaffNumber= gotBasket.getString("clientcode");
etPassenger.setText(""+ gotPassenger );
etStaffNumber.setText("" + gotStaffNumber);
if i try and open the activity without passing the strings It throws a Null pointer exception
right, becuase you didnt send any data
check if the gotBasket is NULL before assigning
like this:
Bundle gotBasket = getIntent().getExtras();
if(gotBasket != null){
gotPassenger= gotBasket.getString("passenger");
gotStaffNumber= gotBasket.getString("clientcode");
etPassenger.setText(""+ gotPassenger );
etStaffNumber.setText("" + gotStaffNumber);
}
Instead of:
Bundle gotBasket = getIntent().getExtras();
better use this:
if(getIntent().hasExtras("passenger")){
//get Extras here
}
That way, you wont get NPE as you only try to get the Bundle Extras only if they were passed
InboxDetailActivity.java:
Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);
Compose.java:
Intent i = getIntent();
Bundle b = i.getExtras();
to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");
I am getting NullPointerException at to = b.getString("To");
Bundle b = i.getExtras();
getExtras() returns null.
Agree with John's answer adding possible solution.
What you are doing is create a bundle , insert values in this and then pass this bundle.
And than you just fetch all the values one by one using its keys.
I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.
I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.
using this instead of bundle
i.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
i.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
i.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
and in another class..
to = getIntent().getString("To");