when i trying to get back from second activity to the main activity, there is something error happens when closing the second activity.
the app in the emulator closed suddenly.
here is the code from second activity:
Intent Data = new Intent();
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nametxt = txt.getText().toString();
String spintxt = spin.getSelectedItem().toString();
Data.putExtra("name" , nametxt);
Data.putExtra("country" , spintxt);
setResult(Activity.RESULT_OK , Data);
finish();
}
});
and here is the code from Main activity:
log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Login.this , Login2.class);
activityResultLaunch.launch(i);
}
});
the rest of the code:
ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
name = data.getExtras().getString("name");
country = data.getExtras().getString("country");
}
}
});
Edit : the Exception from logcat tab:
2022-10-01 22:44:54.281 9270-9270/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 9270
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=921364964, result=-1, data=Intent { (has extras) }} to activity {com.example.myapplication/com.example.myapplication.Login}: java.lang.IllegalStateException: You must either set a text or a view
at android.app.ActivityThread.deliverResults(ActivityThread.java:5347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5386)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.IllegalStateException: You must either set a text or a view
at com.android.internal.util.Preconditions.checkState(Preconditions.java:215)
at android.widget.Toast.show(Toast.java:194)
at com.example.myapplication.Login$4.onActivityResult(Login.java:89)
at com.example.myapplication.Login$4.onActivityResult(Login.java:78)
at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:413)
at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:370)
at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:770)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:8613)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5340)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5386)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)