Getting Null values while Passing datas through Intent to another activity - android

I am getting null Values while passing datas through Intent from 1 activity to another activity.
Passing from 1Activity:
int s = position;
String str=adapter.getId(position);
int Type=DeviceType;
Bundle bunch=new Bundle();
bunch.putString("id", str);
bunch.putInt("DeviceType",Type);
bunch.putInt("position", s);
Intent It=new Intent();
It.setClass(PYSActivity.this,GridImages.class);
It.putExtras(bunch);
startActivity(It);
Retriveing here in 2 Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Bundle b = this.getIntent().getExtras();
AppID=b.getString("id");
DeviceType=b.getInt("DeviceType");
Position=b.getInt("position");
list=(GridView)findViewById(R.id.list);
adapter1=new LazyAdapter1(this,mStrings,like,Rate,Id,img);
list.setAdapter(adapter1);

Do something like this:
Activity 1:
int myInt = 5;
String myString = "hi";
...
Intent Intent = new Intent(...);
intent.putExtra("string_key", myString);
intent.putExtra("int_key", myInt);
startActivity(intent);
Activity 2:
int getInt;
String getString;
...
Bundle extras = getIntent().getExtras();
// Read the extras data if it's available.
if (extras != null)
{
getInt = extras.getInt("int_key");
getString = extras.getString("string_key");
}

Why you are creating bundle just use intent.
it.putExtra("id", str);
it.putExtra("DeviceType",Type);
it.putExtra("position", s);

Related

Passing variable between activities through intent throws NullPointerException

Why does this throw NullPointerException ?
In Redeem.java
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", yourInt);
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score = extras.getInt("intVariableName");
Try with:
Bundle extras = getIntent().getExtras();
String stringScore = extras.getString("intVariableName");
int score = Integer.parseInt(stringScore);
Or:
int score = intent.getIntExtra("intVariableName", 0);
You can also try this:
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", String.valueOf(yourInt));
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score =Integer.parseInt(extras.getString("intVariableName"));
Use this:
getIntent().getIntExtra("intVariableName", 0); //where 0 is default value

passing intent value to another activity

Why the value did not appear in the second activity which is consultDoctorAnaemia? I think the code is already correct. But it display null.
resultAnaemia
if(symptom16.equals("Yes"))
{
weight=0.11;
newWeight = 0.0 * 0.15; //cf disease = 0.6, [min=0.15]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultAnemia.this, consultDoctorAnaemia.class);
intent.putExtra("cfDiseases", cf);
startActivity(intent);
}
consultDoctorAnaemia
TextView textView = (TextView) findViewById(R.id.textCF);
//get passed intent
Intent intent = getIntent();
if(null != intent)
{
//get cf value from intent
String cf = intent.getStringExtra("cfDiseases");
textView.setText("Certainty value : " + cf);
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
String Diseases = extras.getString("cfDiseases");
}
You need to do in the consultDoctorAnaemia activity:
Bundle bundle = getIntent().getExtras();
String value2 = bundle.getString("cfDiseases");
Try this in first activity:
// do your intent setup somewhere and then setup bundle
Bundle info = new Bundle();
info.putString("cfDiseases", cf);
intent.putExtras(info);
startActivity(intent);
In new activity:
Bundle info = new Bundle();
info = getIntent().getExtras();
cf = info.getString("cfDiseases");
You can also pass value like this way
Declare your string global and static For example
Declare in variable in this class
Class A
public class A extends Activity{
static String cf = "abcde";
}
Access variable in this B class
class B
public class B extends Activity{
String Temp;
//you can get variable like this
Temp=A.cf ;
Toast.makeText(B.this, "Temp = "+Temp, Toast.LENGTH_SHORT).show();
}

How to pass integer value between activities using intent ?

I've done the coding as below...
Activity1
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R", num);
startActivity(intent);
Activity 2
v=getIntent().getIntExtra("R",2);
the value v should be passed to a switch.. v has 4 values...
But value of v is not changing.. always the same.. anything wrong about code ?
In Activity 1:-
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R", "anyvalue");
startActivity(intent);
In Activity2:-
Public String r;
in OnCreate()
Bundle b = getIntent().getExtras();
r = b.getString("R");
Then again use Intent on Button click
Intent intent=new Intent(Activity2.this, Activity3.class);
intent.putExtra("rr", r);
startActivity(intent);
In Activity3 :-
Public String s;
in onCreate()
Bundle b = getIntent().getExtras();
s = b.getString("rr");
Activity1:
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R",String.valueOf(num));
startActivity(intent);
Activity 2:
Intent intent = getIntent();
v= Integer.parseInt(intent.getStringExtra("R"));
Activity 1
String numstr=num.toString();
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R", numstr);
startActivity(intent);
Activity 2
String vstr=getIntent().getExtras().getString("R");
int v=Integer.parseInt(vstr);
The logic here is simple.Just convert the integer to string and pass it.Then on the other class recieve it as string and then convert it into integet using parseInt().
In Acitvity 1
Intent pass = new Intent(Activity2.this);
Bundle data = new Bundle();
data.putInt("pos", position);
pass.putExtras(data);
startActivity(pass);
In Activity 2 in your on create method
Bundle extras = getIntent().getExtras();
if(extras != null){
position = extras.getInt("pos", position);
}

How to send String[] imageUrl to another activity

I am having array of image URL.On button click I want to send selected image URL to another activity.
Main.java
String[] imageUrl={"https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png", "https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("******");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
});
OpenImage.java
ImageView image = (ImageView)findViewById(R.id.imageview);
What to write here next
In your First Activity,
String[] data = {"Hello", "World"};
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("some_key", data);
startActivity(intent);
Then in you Sencon Activity,
// At class level
private static final String TAG = SecondActivity.class.getSimpleName();
// In onCreate
String[] data = getIntent().getExtras().getStringArray("some_key");
for (String x : data) {
Log.i(TAG, x);
// Toast to display all you values one by one
Toast.makeText(SecondActivity.this, x, Toast.LENGTH_SHORT).show();
}
Hope this helps...:)
Try This:
Bundle bundel = new Bundle();
bundel.putStringArray("key",array);
Intent intent = new Intent(this,next.class)
intent.putExtras(bundel);
startActivity(intent);
or just
intent.putExtra("strings", myStrings);
the putExtra has many overloads, pass array of primitive type is one of them :)
Use This ti send another Activity...
Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
Bundle bundle = new Bundle();
bundle.putStringArray("ArrayURL", imageUrl);
intent1.putExtras(bundle);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
and For Getting
Bundle b = getArguments();
Cat_Name = b.getStringArray("ArrayURL");

How to store and retrieve Array-list value in Android?

In Android, Using Bundle to store and retrieve Array-list value, but i cannot retrieve this array-list value, please help me
Thanks in Advance
I tried this:
ArrayList<String> al = new ArrayList<String>();
al.add(""+StrValue);
Bundle value= new Bundle();
value.putStringArrayList("temp1", al);
ArrayList<String> al = new ArrayList<String>();
Bundle bundle =new Bundle();
System.out.println("Retrieve Values are: "+bundle.getStringArrayList("temp1"));
Finally i got the result is Retrieve Values are (null)
Intent i = new Intent(this,name.class);
i.putStringArrayListExtra("stringname", value);
startActivity(i);
And get data into Tabbar activity like
Bundle extras = getIntent().getExtras();
if (extras != null) {
ArrayList<String> value = extras.getStringArrayList("stringname");
Log.e(" array list value ", "" + value.size());
}
And tab bar set Intent like
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("text");
Intent intent = new Intent(this, TargetActivity.class);
intent.putStringArrayListExtra("string", value);
spec.setContent(intent);
tabHost.addTab(spec);
And get data in to TargetActivity like,
Bundle extras = getIntent().getExtras();
if (extras != null) {
ArrayList<String> value = extras.getStringArrayList("string");
Log.e(" array list value ", "" + value.size());
}
You are initializing a new Bundle. You should use the same bundle in which you have stored.
Bundle bundle =new Bundle();
Instead do
Bundle bundle = value;

Categories

Resources