In activity 1:
package com.example.project1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button button1, button2;
private RadioGroup rd;
private RadioButton checkmass, checktemp, checklen;
public int val = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.bt1);
button2 = (Button) findViewById(R.id.bt2);
rd = findViewById(R.id.rg1);
RadioButton checkMass, checkTemp, checkLen;
checkMass = findViewById(R.id.rb1);
checkLen = findViewById(R.id.rb2);
checkTemp = findViewById(R.id.rb3);
Intent intent1 = new Intent(this, MainActivity2.class);
Intent intent2 = new Intent(this, MainActivity3.class);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkMass.isChecked() || checkLen.isChecked() || checkTemp.isChecked()) {
if(checkMass.isChecked())
val = 1;
else if(checkLen.isChecked())
val = 2;
else if(checkTemp.isChecked())
val = 3;
intent1.putExtra("val",val);
Toast.makeText(MainActivity.this, val, Toast.LENGTH_SHORT).show();
openActivity2();
} else {
Toast.makeText(MainActivity.this, "Choose one of the three choices", Toast.LENGTH_SHORT).show();
}
}
private void openActivity2() {
intent1.putExtra(isc, val);
startActivity(intent1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity3();
}
private void openActivity3() {
startActivity(intent2);
}
});
}
}
in Activity 2:
int val = getIntent().getIntExtra("val", 0);
The debugger gives this:
E/xample.project: Invalid ID 0x00000001.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.project1, PID: 26709
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.Toast.makeText(Toast.java:307)
at com.example.project1.MainActivity$1.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 26709 SIG: 9
CodePudding user response:
val
is a number which is treated by makeText
as a resource ID. Turn it into a String: String.valueOf(val)
. Pass this expression to makeText
instead if val
itself.