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();
}
Related
I'm new on Android and I want to know how I can use Serializable in my code
First Activity:
#Override
public void onItemClick(Neighbour item) {
Intent i = new Intent(getActivity(), ProfilNeighbourActivity.class);
i.putExtra("avatar", item.getAvatarUrl());
i.putExtra("name", item.getName());
i.putExtra("city", item.getAddress());
i.putExtra("phone", item.getPhoneNumber());
i.putExtra("about", item.getAboutMe());
i.putExtra("fbUrl", item.getFbUrl());
startActivity(i);
}
Second Activity:
mAvatar = findViewById(R.id.profil_neighbour_image);
mName1 = findViewById(R.id.profil_neighbour_name1);
mName2 = findViewById(R.id.profil_neighbour_name2);
mCity = findViewById(R.id.profil_neighbour_city);
mPhone = findViewById(R.id.profil_neighbour_phone);
mAbout = findViewById(R.id.profil_neighbour_about);
mFbUrl = findViewById(R.id.profil_neighbour_fbUrl);
Intent i = getIntent();
String image = i.getStringExtra("avatar");
String name = i.getStringExtra("name");
String city = i.getStringExtra("city");
String phone = i.getStringExtra("phone");
String about = i.getStringExtra("about");
String fbUrl = i.getStringExtra("fbUrl");
Glide.with(this).asBitmap().load(image).fitCenter().into(mAvatar);
mName1.setText(name);
mName2.setText(name);
mCity.setText(city);
mPhone.setText(phone);
mFbUrl.setText(fbUrl + name);
mAbout.setText(about);
How can I shorten my code with Serializable please?
you can use passing objects around then Parcelable
more details : Parcelable
I am trying to learn Android Studio and my first app is a blood alcohol calculator. The user starts that app and then a new activity is started so that they can enter their weight and press ok, this returns them back to the main activity and fills in the weight text.
I use startActivityForResult and then putExtra in the second activity. The first activity crashes if I use the getExtra method, if I delete the 2 receiving lines of code then there is no crash. When I use the debugger it says NullPointerException just before it says App has stopped working
Main activity code
public class MainActivity extends Activity {
static int displayunit = 0;
static double percentage = 5.0;
static int change = 1;
static double bah;
static double vol = 25;
static double timestamp = 0;
static double w = 0;
static String we = "a";
final int rcode = 3;
final String[] units = {"Small Shot(25ml)", "Large Shot(35ml)", "Small
Port/Sherry(50ml)", "Large Port/Sherry(70ml)", "Small Wine(125ml)",
"Large
Wine(175ml)", "Small Beer Bottle(284ml)", "Half Pint(236.6ml)", "Medium
Beer Bottle(330ml)", "Can of beer(440ml)", "Large Bottle(500ml)",
"Pint(568.26ml)", "Massive Beer Bottle(660ml)"};
final int[] unitsum = {25, 35, 50, 70, 125, 175, 284, 237, 330, 440, 500,
569, 660};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setClassName("com.example.alccalc","com.example.alccalc.enterweight");
if(w ==0){
startActivityForResult(intent, rcode);
}
}
#Override
protected void onActivityResult ( int requstCode, int resultCode,
Intent intent){
if (requstCode == rcode && resultCode == RESULT_OK) {
we = getIntent().getStringExtra("weighttext");
w = Double.parseDouble(we);
}
TextView kg = (TextView) findViewById(R.id.kg);
kg.setText(we)
Second Activity
public class enterweight extends Activity {
EditText entweight;
TextView tester;
String weightstring;
#Override
protected void onCreate(Bundle State) {
super.onCreate(State);
setContentView(R.layout.activity_enterweight);
entweight = (EditText) findViewById(R.id.entweight);
tester = (TextView)findViewById(R.id.tester);
Button okweight = (Button) findViewById(R.id.okweight);
okweight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
weightstring = entweight.getText().toString();
//tester.setText(weightstring);
Intent intent = new Intent();
intent.putExtra
("weighttext", weightstring);
setResult(RESULT_OK, intent);
if (intent.hasExtra("weighttext")) {
finish();
}
}
});
}
}
The error is here getIntent();
Since you are using the onActivityResult ( int requstCode, int resultCode,
Intent intent) method, you can find your extras in the variable intent. The method getIntent() returns the intent that is used to start the activity. In your case it is null.
Use:
we = intent.getStringExtra("weighttext");
instead of
we = getIntent().getStringExtra("weighttext");
Better:
Bundle extras = intent.getExtras();
if (extras != null) {
String result = extras.getString("weighttext");
.....
}
Try this in your receiving code
Intent intent= getIntent();
Bundle b = intent.getExtras();
if(b != null)
we = b.getString("weighttext");
You are calling getIntent() to get the result data. getIntent() is an activity method that returns an intent that is used to start the activity. You should use the intent variable that is passed on onActivityResult method.
if (requstCode == rcode && resultCode == RESULT_OK) {
we = intent.getStringExtra("weighttext");
w = Double.parseDouble(we);
}
This is the first activity
Intent intent = new Intent(Songs.this,PlayingSong.class);
Bundle bundle=new Bundle();
bundle.putStringArray("data",songs);
intent.putExtras(bundle);
startActivity(intent);
This is the second activity
tv = (TextView)findViewById(R.id.playingSongName);
imge = (ImageView)findViewById(R.id.playingImage);
Bundle bundle = getIntent().getExtras();
String[] arrRecd = bundle.getStringArray("data");
tv.setText(arrRecd[]);
You can't set a String Array directly to the TextView. Instead create a String and set it to the TextView.
String text = "";
for(int i = 0; i < arrRecd.length; i++) {
text = text + arrRecd[i];
}
tv.setText(text);
In First Activity :
Intent intent=new Intent(this,SearchActivity.class);
intent.putStringArrayListExtra("data",songs);
startActivity(intent);
In second activity:
ArrayList<String> list=getIntent().getStringArrayListExtra("data");
I have in android a string, and in this string there is an url.
So now Iput that string into a put.extra, because I want to give that string to an 2. Activity.
Now in the 2. Activity I want to recive my string and use that url , which is in the string, for a new/other string.
How can I done this??
For myself I did an example which you can see under this , but when I run this App get a blank page in the Second Activity! Whats wrong in this code??
Code example:
Activity String definition:
private static final String TAG_PURL = "url";
Extra and new Intent:
Bundle bundle = new Bundle ();
bundle.putSerializable(TAG_PURL, purl);
// Starting new intent
Intent postin = new Intent(getApplicationContext(), post.class);
postin.putExtras(bundle);
startActivity(postin);
}
});
Reciving Extra:
Intent postin = getIntent();
Bundle bundle = this.getIntent().getExtras();
String purl = (String) bundle.getSerializable (TAG_PURL);
Use of the purl String in a new String:
private static final String url = TAG_PURL ;
You can try this if you want:
private static final String TAG_PURL = "url";
Intent i = new Intent(A.this, B.class);
i.putExtra("URL", TAG_PURL);
startActivity(i);
You can get this from your activity b like:
Bundle extras = getIntent().getExtras();
String url= extras.getInt("URL");
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);