Startactivity for result with causes crash - android

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);
}

Related

Identify a specific dynamically generated TextView in order to setText

I have a function that generates a specific number of TextViews dynamically.
TextView tv;
EditText et;
public TextView textViewGenerate(final Activity activity, String tag, Integer id) {
tv = new TextView(activity);
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFFFFFF);
gd.setCornerRadius(4);
gd.setStroke(1, 0xFF757575);
tv.setBackground(gd);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f
);
tv.setPadding(7, 9, 0, 0);
tv.setGravity(Gravity.START);
tv.setTextSize(22);
tv.setTag(tag);
tv.setId(id);
tv.setHint("Enter Module Serial Number");
//Click to launch camera
tv.setClickable(true);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent = new Intent(ct, MctCameraActivity.class);
Intent intent = new Intent(activity, MctCameraActivity.class);
activity.startActivityForResult(intent, 1);
//ct.startActivity(intent);
//ToDo: finish receiving the data from the activityForResult
}
});
lp.setMargins(10, 0, 10, 0);
lp.setMarginStart(10);
lp.setMarginEnd(10);
tv.setLayoutParams(lp);
return tv;
}
}
The onClick launches another activity that loads the camera in order to capture barcode data. In the main activity I create the TextViews with
final LayoutElements le = new LayoutElements();
mainLayout = (LinearLayout)findViewById(R.id.mctScanPageMain);
for(int i = 0; i < 3; i++) {
mainLayout.addView(le.textViewGenerate(this, "NewID" + i, i));
}
The onActivityResult returns the value from the camera activity but the issue is that it sets every generated TextView to have the same barcode number (the barcode number is returned as a string). How can I modify this so that only the text of the selected TextView gets changed in the onActivityResult?
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null) {
String returnResult = data.getStringExtra("result");
for (int i=0; i < mainLayout.getChildCount(); i++){
TextView tv = (TextView) mainLayout.findViewWithTag("NewID"+i);
if(tv == null) {
break;
} else {
tv.setText(returnResult);
}
}
}
}
In your click listener of TextView you can change:
Intent intent = new Intent(activity, MctCameraActivity.class);
activity.startActivityForResult(intent, 1);
to
Intent intent = new Intent(activity, MctCameraActivity.class);
intent.putExtra("text_view_tag", tag);
activity.startActivityForResult(intent, 1);
And then sent it back in result of MctCameraActivity, so you can use it in onActivityResult with getStringExtra. This way you will know tag of TextView which started activity.
Alternatively you can use requestCode that you pass to startActivityForResult to differentiate between text views. For example pass request code 1 for text view 1, request code 2 for text view 2 etc and use this requestCode in onActivityResult.

Android BMI Calculator App error in intent

I am trying to build a BMI calculator app, however, there seems to be a problem with using intent and the method getIntExtra(), as I always ended up getting the default value and not the value i passed in from another activity.
Below is my code for the first activity
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
int weight = Integer.parseInt(weightText.getText().toString());
int height = Integer.parseInt(heightText.getText().toString());
intent.putExtra(USER_WEIGHT_EXTRA, weight);
intent.putExtra(USER_HEIGHT_EXTRA, height);
startActivity(intent);
}
});
Second Activity
the reason for double height2 = (double)height/100 is to convert from cm to m.
result = (TextView)findViewById(R.id.result);
todo = (TextView) findViewById(R.id.todo);
Intent intent = getIntent();
int weight = intent.getIntExtra("USER_WEIGHT_EXTRA", extraInt);
int height = intent.getIntExtra("USER_HEIGHT_EXTRA", extraInt);
double height2 = (double)height/100;
double BMI = (weight*1.0)/(height2*height2);
if (BMI < 20.0) {
result.setText("You are: UNDERWEIGHT");
todo.setText("You Should EAT MORE");
} else if (BMI > 20.0 && BMI < 25.0) {
result.setText("You are: NORMAL WEIGHT");
todo.setText("You Should keep STAYING HEALTHY");
} else if (BMI > 25) {
result.setText("You are: OVERWEIGHT");
todo.setText("You Should EXERCISE MORE");
}
}
I am really stuck on this issue. Thank you all so much!
In first activity
Intent in = new Intent(this, OtherActivity.class);
in.putExtra("USER_WEIGHT_EXTRA", weightText.getText().toString());
in.putExtra("USER_HEIGHT_EXTRA", heightText.getText().toString());
startActivity(in);`
for otherActivity
Bundle bundle = getIntent().getExtras();
int weight = Integer.parseInt(bundle.getString("USER_WEIGHT_EXTRA"));
int height = Integer.parseInt(bundle.getString("USER_HEIGHT_EXTRA"));

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 update a textview

simple program: 2 buttons (previous/next) and a textview to show text.
by intent I created an Index (inside a method)
private void index(){
Intent i = new Intent(this, Index.class);
startActivityForResult(i, 1);
}
Index.class (with 3 buttons):
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String result = "1";
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
}
});
Main.class
String value = "1";
final String prog1[] = new String[16];
final String prog2[] = new String[105];
final String prog3[] = new String[66];
int a;
int b;
int c=3;
int array1start = 0; int array1end = 15;
int array2start = 0; int array2end = 105;
int array3start = 0; int array3end = 65;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (value.equals("1")){
a = array1start;
b = array1end;
prog=prog1;
}
else if (value.equals("2")){
a = array2start;
b = array2end;
prog=prog2;
textView1.setText(""+prog[a]);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
value=result;
Toast toast2=Toast.makeText(this,"value: "+value,Toast.LENGTH_LONG);
toast2.show();
}
if (resultCode == RESULT_CANCELED) {
//Write your code on no result return
}}
}//onAcrivityResult
at this point, choosen choice in index class should be change a result string to "value" string in main class
private void index(){
Intent i = new Intent(this, Index.class);
startActivityForResult(i, 1);
}
my textview take data from array1 or array2 by index class
so, I dont' understand how update textview (because index value is correct).
thanks for the help
Put you code into function onActivityResult
if (value.equals("1")){
a = array1start;
b = array1end;
prog=prog1;
}
else if (value.equals("2")){
a = array2start;
b = array2end;
prog=prog2;
textView1.setText(""+prog[a]);

How do i send multiple Integers to a new activity?

I have a way that I know is not the best way of sending them over rite now but I'm sending them as strings and converting them to an Int on the reciver side, the problem is when I do the conversion it crashes on my phone. This is what I have on my sender side:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// hourly wage send
EditText editText = (EditText) findViewById(R.id.hourly_wage);
String message1 = editText.getText().toString();
intent.putExtra(MESSAGE_1, message1);
// ot wage send
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
String message2 = editText1.getText().toString();
intent.putExtra(MESSAGE_2, message2);
// hours per day send
EditText editText2 = (EditText) findViewById(R.id.hours_day);
String message3 = editText2.getText().toString();
intent.putExtra(MESSAGE_3, message3);
// start new activity
startActivity(intent);
And this is what is on my reciving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
String message1 = intent.getStringExtra(Options.MESSAGE_1);
String message2 = intent.getStringExtra(Options.MESSAGE_2);
String message3 = intent.getStringExtra(Options.MESSAGE_3);
// convert string to integer
int HW = Integer.valueOf(message1);
int OTW = Integer.valueOf(message2);
int HPD = Integer.valueOf(message3);
Ive tested everything and its the conversion that is causing the app to crash, i was hoping somebody could help me make it not crash or give me a whole new way sending an int to my second activity insted of sending a string and converting it.
Thank you!
=======================================================================
Here is my new code with all your help!
Sending:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// Gather text from text boxes
EditText editText = (EditText) findViewById(R.id.hourly_wage);
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
EditText editText2 = (EditText) findViewById(R.id.hours_day);
//Create String from text
String message1 = editText.getText().toString();
String message2 = editText1.getText().toString();
String message3 = editText2.getText().toString();
//Convert String to Int
int HW = 0, OTW = 0, HPD = 0;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
// Send Integers to PayTracker.java
intent.putExtra(MESSAGE_HW, HW);
intent.putExtra(MESSAGE_OTW, OTW);
intent.putExtra(MESSAGE_HPD, HPD);
// start new activity
startActivity(intent);
}
Receiving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra(Options.MESSAGE_HW, 0);
int OTW = intent.getIntExtra(Options.MESSAGE_OTW, 0);
int HPD = intent.getIntExtra(Options.MESSAGE_HPD, 0);
// set textview
TextView textView = (TextView) this.findViewById(R.id.yourpay);
textView.setText(String.valueOf(HW));
}
You don't need to pass Integers as Strings to be converted back in the receiving Activity. Intents can hold Integers as well as Strings.
Simply add your data like you normally would:
int foo = 5;
Intent intent = new Intent(this, bar.class);
intent.putExtra("foobar", foo);
And then retrieve your int from the intent in the receiving Activity as follows.
Intent intent = getIntent();
int foo = intent.getIntExtra("foobar", 0);
Intents can hold more data than just Strings. In fact, take a look at the documentation. You can see that Intents can hold Longs, Doubles, Floats, Parcelables, etc.
Pass data from one activity to another activity "String" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent =getIntent();
Receivevalue =intent.getExtras().getString("TYPE");
(OR)
Receivevalue = getIntent().getExtras().getString("TYPE");
Pass data from one activity to another activity "Integer" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent = getIntent();
value = intent.getIntExtra("TYPE", 0);
// Type = intent.getIntExtra(name, defaultValue);
Try something like this on the send side:
String message1 = editText.getText().toString();
intent.putExtra("MESSAGE_1", message1);
String message2 = editText1.getText().toString();
intent.putExtra("MESSAGE_2", message2);
String message3 = editText2.getText().toString();
intent.putExtra("MESSAGE_3", message3);
Receive side:
if (getIntent() != null) {
if (getIntent().getExtras() != null) {
String mess1 = getIntent().getExtras().getString("MESSAGE_1");
String mess2 = getIntent().getExtras().getString("MESSAGE_2");
String mess3 = getIntent().getExtras().getString("MESSAGE_3");
}
}
You can achieve your goal using following code
In Your Calling Activity
Intent value = new Intent(YourCallingActivity.this,DestinationActivity.class);
value.putExtra("integerone", integeronevalue);
value.putExtra("integertwo", integertwovalue);
value.putExtra("integerthree", integertwovalue);
startActivity(value);
In Destination Activity
int integerone = getIntent().getExtras().getInt("integerone");
int integertwo = getIntent().getExtras().getInt("integertwo");
int integerthree = getIntent().getExtras().getInt("integerthree");
Hope it helps
Encapsulate the conversion part within try/catch block, because your strings may not be convertible into integer values.
int HW, OTW, HPD;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
*Or more appropriately, pass the integer values in your sending part and receive them as it is.

Categories

Resources