从5.0版本的收件箱中获取短信时,应用程序崩溃。在onViewCreated()方法

时间:2018-01-17 05:40:36

标签: android sms

请找到k的代码。这是一个片段。请帮忙。它只在android version 5.0崩溃。如果你想我也可以分享Play商店控制台的详细信息。

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle(getString(R.string.dnd_caps));
//  getListView().addHeaderView(mHeader);
    if (((DNDMasterActivityNEW) getActivity()).checkPermissions()) {
        dialogWait = new DialogWait(getActivity());
        dialogWait.setCancelable(false);
        dialogWait.show();
        try {
            SubscriptionManager subscriptionManager = SubscriptionManager.from(getActivity().getApplicationContext());
            List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
            for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
                int subscriptionId = subscriptionInfo.getSubscriptionId();
                int simSlotIndex = subscriptionInfo.getSimSlotIndex();
                String simNumber = subscriptionInfo.getNumber();
                String carrierName = subscriptionInfo.getCarrierName().toString();
                String displayName = subscriptionInfo.getDisplayName().toString();
                Log.d("DND_SUBS", "subscriptionId:" + subscriptionId);
                Log.d("DND_SUBS", "simslotIndex:" + simSlotIndex);
                Log.d("DND_SUBS", "simNumber:" + simNumber);
                Log.d("DND_SUBS", "carrierName:" + carrierName);
                Log.d("DND_SUBS", "displayName:" + displayName);
                hm.put(subscriptionId, simSlotIndex);
            }
        } catch (NoClassDefFoundError | Exception e) {
            e.printStackTrace();
        }

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext().getApplicationContext());
        Boolean isAppRegistered = settings.getBoolean(Global.isAppRegistered, false);
        realm = Realm.getDefaultInstance();
        RealmResults<trai.gov.in.dnd.ml.model.Sms> smses = getAllSms();
        ArrayList<trai.gov.in.dnd.ml.model.Sms> smsArrayList = new ArrayList<>();
        smsArrayList.addAll(realm.copyFromRealm(smses));
     //   AsyncTaskRunner asyncTaskRunner = new AsyncTaskRunner();
     //   asyncTaskRunner.execute();
        dialogWait.dismiss();
        if (!smsArrayList.isEmpty()) {
            adapter = new SmsListAdapterRecyclerView(getActivity(), smsArrayList);
            recyclerView.setVisibility(View.VISIBLE);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(),
                        LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setHasFixedSize(false);
            recyclerView.setLayoutFrozen(false);
            recyclerView.setItemViewCacheSize(30);
            recyclerView.setDrawingCacheEnabled(true);
            recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(adapter);
        } else {
            ll_probable_spam.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            no_sms.setVisibility(View.VISIBLE);
        }
    }
}

我只是想知道这条线是否也会崩溃

@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)  

或应用崩溃的原因

请找到附加的堆栈跟踪

    java.lang.NoSuchMethodError: 
  at trai.gov.in.dnd.app.SmsFragment.onViewCreated (SmsFragment.java:97)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1314)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1528)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1595)
  at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:758)
  at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2363)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2149)
  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps (FragmentManager.java:2103)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2013)
  at android.support.v4.app.FragmentManagerImpl$1.run (FragmentManager.java:710)
  at android.os.Handler.handleCallback (Handler.java:739)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:135)
  at android.app.ActivityThread.main (ActivityThread.java:5233)
  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:898)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:693)

2 个答案:

答案 0 :(得分:2)

问题在于SubscriptionManager

SubscriptionManager sm=SubscriptionManager.from(getActivity().getApplicationContext());
  

调用需要API级别22(当前最小值为19):android.telephony.SubscriptionManager)

您必须在致电前检查Build版本。以下是根据您的需要修改它的示例。

 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
public static String getSubscriptionInfo(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
        return "Not supported";
    if (!((DNDMasterActivityNEW) getActivity()).checkPermissions())
        return "No permission";
    StringBuilder stringBuilder=new StringBuilder();
    SubscriptionManager sm = SubscriptionManager.from(context);
    // Do your task here
    return stringBuilder.toString();
}

注意: - 请勿在{{1​​}}中添加@TargetApi注释,这样做无用。 onViewCreated()是片段的生命周期方法,它意味着被调用 的修改  您可以先检查可用性,然后再继续。如

onViewCreated()

答案 1 :(得分:1)

解决方案就在这里

订阅类在6.0​​以下不可用,因此app崩溃了。我为版本5.0和5.1删除了它,它工作正常。感谢