I startActivityForResult trying to create a new activity, and in it startActivityForResult in this way:

  Activity activity = new Activity() { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.i("RLOG", "result: " + resultCode); } }; activity.startActivityForResult(intent, 8700); 

But it knocks out such an error:

 01-28 21:44:25.158 23961-23961/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.rostislav.dugin.osloader, PID: 23961 rx.exceptions.OnErrorNotImplementedException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386) at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383) at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44) at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157) at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120) at rx.Observable.subscribe(Observable.java:8770) at rx.Observable.subscribe(Observable.java:8726) at rx.Observable.subscribe(Observable.java:8549) at com.rostislav.dugin.osloader.ui.presenter.SettingsPresenter.onInstallBochsClick(SettingsPresenter.java:95) at com.rostislav.dugin.osloader.ui.view.fragments.SettingsFragment.lambda$setUpViews$3(SettingsFragment.java:119) at com.rostislav.dugin.osloader.ui.view.fragments.SettingsFragment$$Lambda$3.onClick(Unknown Source) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20260) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5624) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference at android.app.Activity.startActivityForResult(Activity.java:3810) at android.app.Activity.startActivityForResult(Activity.java:3761) at com.rostislav.dugin.osloader.util.RawApkInstaller.lambda$installApk$0(RawApkInstaller.java:40) at com.rostislav.dugin.osloader.util.RawApkInstaller$$Lambda$1.call(Unknown Source) at rx.Observable.subscribe(Observable.java:8759) at rx.Observable.subscribe(Observable.java:8726) at rx.Observable.subscribe(Observable.java:8549) at com.rostislav.dugin.osloader.ui.presenter.SettingsPresenter.onInstallBochsClick(SettingsPresenter.java:95) at com.rostislav.dugin.osloader.ui.view.fragments.SettingsFragment.lambda$setUpViews$3(SettingsFragment.java:119) at com.rostislav.dugin.osloader.ui.view.fragments.SettingsFragment$$Lambda$3.onClick(Unknown Source) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20260) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5624) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

How to fix it and run the method in such an activity?


I need this to write a class that is used in Presenter and transfer activity through 4 layers of classes, changing the Observable code is not an option at all (and not because I don’t know what to do wrong).

  • 2
    Obviously creating an instance of activation is a very bad and wrong idea. - post_zeew
  • one
    No, look for another way to solve your problem. - temq

2 answers 2

In general, as a result of communication in the comments, we came to the conclusion that it is impossible to start an Activity in this way, in any case we need a running instance.

The only way to start startActivityForResult is to get a link to the running Activity and execute the method through it.

    Oh tin!

    Activity cannot be created the way you do it. Android capricious, and you can only ask him about something, but not ordered. For example, if you want to ask the system to start a new activity (which class you really have in the project), then at least you need to ask the class implementing Context to start this activity for you (inside, for example, your MainActivity)

     class MainActivity extends AppCompatActivity { @Override public void onCreate(...) { ... startActivityForResult(new Intent(this, SomeOfYourActivity.class), 8700) } } 

    And in general, read something about android. Urgently!

    • Well, you do not immediately take me for a fool :). I know that doing so badly, but hoping for the best. I create ... this is due to the fact that I need to use startActivityForResult in a class designed for a specific purpose, where accompanying a call through an Activity, then Fragment, then Presenter, then a class and sticking calls into Observable is not an option at all. - user189127
    • If I understand you correctly, then in this class you will need a Context, in which you will call startActivityForResult. One more thing, you need to specify in the event that the activity should be running. I don’t take it as a fool, because I saw rx.Observables in the stektrais, which indicates that it’s in the android novice, and not in the whole programming 😉. You are creating an anonymous activity class, but this will not work. It is also necessary to register it in the manifesto ... - Vladymyr R.tmnko
    • And again by! :) I have indicated in the Intent'e (unless it is not the activity, but the launch of the application installation) And I wanted to start Context , but, first, it does not have a startActivityForResult method, and, secondly, I will have to create it in a similar way. - user189127
    • By the way, I learned about the Observable in Android. And in general, I got half of my experience in Android. I can’t be called a newbie at all, I hope, I’m already down to a weak joon, but I’m trying to write something more or less serious recently. And I am only 15 years old. - user189127
    • I apologize, I do not need a context and FragmentActivity. - Vladymyr R.tmnko