首页 > 解决方案 > startActivityForResult 应仅在暗模式下使用错误

问题描述

我正在我的应用程序上实现暗模式,我面临一个特殊的(对我来说)问题。下面是logcat崩溃的情况,我在 中面临dark mode,在 Light/Default 中没有错误/警告(我在 中SDK<=29,所以我的默认设置是省电模式)。即使开启了省电模式,即应用程序基本上处于黑暗模式,也不会发生这种崩溃。

错误日志:

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) 

只有当我调用它时(在显式暗模式下),才会发生这种崩溃(在 MainActivity 中调用,而不是在 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());
      }
    }

我已经明确要求在我的片段中检查夜间模式,例如:

@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;
  }

我需要这个检查,因为如果我不放这个,那么这个片段的卡片在开始时是亮的。我在 MainActivity 中也有这段代码onCreate,但这不会产生任何错误。

我完全困惑为什么我会得到这个。请帮忙。

标签: androidandroid-layoutandroid-theme

解决方案


推荐阅读