0

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) 
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    To find out, you need to go to the Logcat tab and look for the stacktrace of the exception that was thrown. – Code-Apprentice Oct 01 '22 at 20:00
  • 2022-10-01 22:08:43.247 8303-8303/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 8303 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1363374537, result=-1, data=Intent { }} to activity {com.example.myapplication/com.example.myapplication.Login}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference – Abdalrahman Oct 01 '22 at 20:10
  • Please edit your question and provide the complete stack trace. – CommonsWare Oct 01 '22 at 20:16
  • what you mean by stack trace @CommonsWare ? – Abdalrahman Oct 01 '22 at 20:23
  • 1
    You can learn more about stack traces [here](https://commonsware.com/Jetpack/pages/chap-debug-001.html), [here](https://www.sentinelone.com/blog/java-stack-trace-understanding/), [here](https://www.javatpoint.com/java-stack-trace), [here](https://rollbar.com/blog/java-stack-trace/#), [here](https://www.twilio.com/blog/how-to-read-and-understand-a-java-stacktrace), and [here](https://stackoverflow.com/q/3988788/115145), among other places. – CommonsWare Oct 01 '22 at 20:27
  • Please [edit] your question with the error message. – Code-Apprentice Oct 01 '22 at 20:46
  • Also read [this](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) and [this](https://stackoverflow.com/questions/13303544/how-to-solve-nullpointerexception-error-in-android) for tips specific to NullPointerException. – Code-Apprentice Oct 01 '22 at 20:47
  • Your code in the question does not match the stack trace. You are crashing when trying to show a `Toast`, and there is no `Toast` in your question – CommonsWare Oct 01 '22 at 21:17
  • i know , i just deleted the Toast code to reduce the size of the code and the question, it is just a message . does it affects during the runtime ? @CommonsWare – Abdalrahman Oct 01 '22 at 21:49
  • "does it affects during the runtime ?" -- well, if you have a bug with your `Toast`, yes. – CommonsWare Oct 01 '22 at 22:01

0 Answers0