startActivityForResult should be used error only in dark mode

大憨熊 提交于 2021-01-28 11:20:54

问题


I am implementing dark mode on my app, and I am facing a peculiar(to me) problem. Below is the logcat of the crash, which I am facing only in dark mode, no error/warning in Light/Default (I am in SDK<=29, so my default is battery saver) mode. Even when battery saver is on, i.e. the app is essentially in dark mode, this crash does not occur.

Log of the error:

2020-02-20 11:20:59.726 16666-16666/com.example.trial E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.trial, PID: 16666
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trial/com.google.android.libraries.places.widget.AutocompleteActivity}: java.lang.IllegalStateException: Cannot find caller. startActivityForResult should be used.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
        at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57)
        at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5238)
        at android.app.ActivityThread.access$3400(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2026)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalStateException: Cannot find caller. startActivityForResult should be used.
        at com.google.android.libraries.places.internal.zzft.zzb(com.google.android.libraries.places:places@@2.2.0:11)
        at com.google.android.libraries.places.widget.AutocompleteActivity.onCreate(com.google.android.libraries.places:places@@2.2.0:8)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279) 
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187) 
        at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57) 
        at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5238) 
        at android.app.ActivityThread.access$3400(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2026) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

This crash is happening only when (with explicit dark mode), when I am calling this (called in MainActivity, not in the Fragment):

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
  case R.id.action_searchplace:
    onSearchCalled();
    return true;
    ....
     *other cases does not show this error*
    ...
}
public void onSearchCalled() {
    // Set the fields to specify which types of place data to return.
    List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG);
    // Start the autocomplete intent.
    Intent intent = new Autocomplete.IntentBuilder(
        AutocompleteActivityMode.OVERLAY, fields) //no .setCountry...Search whole world
        .build(this);
    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {
        Place place = Autocomplete.getPlaceFromIntent(data);
        if (place.getLatLng() !=null) {
          Lat = place.getLatLng().latitude;
          Long = place.getLatLng().longitude;
//          Toast.makeText(MainActivity.this, ""+Lat, Toast.LENGTH_LONG).show();
          setupViewPager();
        } else {
          Toast.makeText(this, getString(R.string.location_not_found), Toast.LENGTH_LONG).show();
        }
      } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
        // TODO: Handle the error.
        Status status = Autocomplete.getStatusFromIntent(data);
        Toast.makeText(MainActivity.this, "Error: " + status.getStatusMessage(), Toast.LENGTH_LONG).show();
//        Log.i("LocationSearch", status.getStatusMessage());
      }
    }

and I have explicitly called for checking night mode in my fragments, like:

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    } else {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    }
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);
//    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    String theme = sharedPref.getString("theme", "Default");
//    Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
    if (theme.equals("Dark")) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else if (theme.equals("Light")) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
      } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
      }
    }
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_sun, container, false);
    ....
    ....

  @Override
  public void onAttach(@NonNull  Context context) {
    super.onAttach(context);
    mContext = context;
  }

I need this checking because, if I don't put this, then the cards of this fragment is light on start. I also have this code in MainActivity's onCreate, but that is not creating any error.

I am totally confused why I am getting this. Kindly help.

来源:https://stackoverflow.com/questions/60313831/startactivityforresult-should-be-used-error-only-in-dark-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!