李堇 2022-06-02 10:04 采纳率: 52.8%
浏览 54
已结题

开发安卓应用出现频繁闪退重启问题

刚接触安卓开发,应用是简单的结构,activity中嵌套fragment,当fragment中的条件按钮改变时,进行重新请求数据和列表展示:

img

代码如下(页面进入后进行一次初始条件查询):

public class SoulPostStationFragment extends PagingFragment<TBKnowledgeCourseVo> implements AdapterView.OnItemClickListener {

    private FragmentManager fManager;

    private TabLayout tab_layout_time;

    private String studentId;

    //查询条件
    private String grade = "0006";
    private String course = "0001";
    private String near2 = "0003";

    public SoulPostStationFragment(FragmentManager fManager) {
        this.fManager = fManager;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_soul_post_station,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        studentId = SharedPreferencesUtil.getString(getContext(), "studentId", null);

        listView = (XListView) view.findViewById(R.id.knowledgeCourseList2);
        listView.setOnItemClickListener(this);
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);
        //initDatas();

        tab_layout_time = view.findViewById(R.id.tab_layout_time);
        tab_layout_time.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中某个tab
                LogUtils.e("=1=","onTabSelected1:" + tab);
                pageIndex = 1;
                near2 = tab.getText().toString();
                if("近一个月".equals(near2)){
                    near2 = "0001";
                }else if("近三个月".equals(near2)) {
                    near2 = "0002";
                }else if("近半年".equals(near2)) {
                    near2 = "0003";
                } else if("更多".equals(near2)) {
                    near2 = "";
                }
                if (alllist != null){
                    alllist.clear();
                }
                initDatas();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //当tab从选择到未选择
                LogUtils.e("=1=","onTabUnselected1:" + tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //已经选中tab后的重复点击tab
                LogUtils.e("=1=","onTabReselected1:" + tab);
                pageIndex = 1;
                near2 = tab.getText().toString();
                if("近一个月".equals(near2)){
                    near2 = "0001";
                }else if("近三个月".equals(near2)) {
                    near2 = "0002";
                }else if("近半年".equals(near2)) {
                    near2 = "0003";
                } else if("更多".equals(near2)) {
                    near2 = "";
                }
                if (alllist != null){
                    alllist.clear();
                }
                initDatas();
            }
        });

        tab_layout_time.getTabAt(2).select();


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        SoulPostStationDetailFragment soulPostStationDetailFragment = new SoulPostStationDetailFragment(fManager);
        LogUtils.e("=1=","list.size :" + alllist.size());
        LogUtils.e("=1=","position :" + position);
        Bundle bd = new Bundle();
        bd.putString("id", alllist.get(position-1).getId());
        bd.putString("fileUrl",alllist.get(position-1).getFileUrl());
        LogUtils.e("=1=","onItemClick id:" + bd.get("id"));
        LogUtils.e("=1=","onItemClick fileUrl:" + bd.get("fileUrl"));
        soulPostStationDetailFragment.setArguments(bd);
        //加上Fragment替换动画
        fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit);
        fTransaction.replace(R.id.centerFragment, soulPostStationDetailFragment);
        //调用addToBackStack将Fragment添加到栈中
        fTransaction.addToBackStack(null);
        fTransaction.commit();
    }

    protected void initDatas() {
        LogUtils.e("=1=","initDatas:");
        if (NetUtils.isConnected(this.getContext())) {
            new queryKnowledgeCourseList().execute();
        } else {
            //NoNetAdapter();
            ToastUtil.showShort(this.getContext(), R.string.net_notConnectedHint);
        }
    }

    protected void onLoad() {
        listView.stopRefresh();
        listView.stopLoadMore();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前系统时间
        String nowTime = df.format(new Date(System.currentTimeMillis()));
        // 释放时提示正在刷新时的当前时间
        listView.setRefreshTime(nowTime);
    }

    class queryKnowledgeCourseList extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... strings) {
            String url = HttpConstant.queryKnowledgeCourseList;
            String postString = "studentId="+studentId+"&mchnId=MX0000001"+"&near2="+near2+"&knowledgeType=0012&pageNo="+pageIndex+"&pageSize="+pageSize;
            LogUtils.e("postString:" , postString);
            String json = HttpConnectionUtil.AccessUrlGET(getContext(),url +"?"+ postString);
            LogUtils.e("json:" , json);
            return json;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            ApiResponse<TBKnowledgeCourseVo> info = JsonUtils.jsonToJavaBean(result, ApiResponse.class);
            if (info.getCode() != null && info.getCode().equals("10000")) {
                list = JsonUtils.jsonToJavaBeanList(JSONArray.toJSONString(info.getData()), TBKnowledgeCourseVo.class);
                alllist.addAll(list);
                KnowledgeCourseAdapter2 mAdapter = new KnowledgeCourseAdapter2(getContext(), alllist);
                listView.setAdapter(mAdapter);
            }
        }
    }
}

但是点击按钮后会不定时出现以下问题

img

错误日志为:

--------- beginning of crash
2022-06-02 10:02:15.811 1514-1514/com.mxjy.ssep E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mxjy.ssep, PID: 1514
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        at java.util.ArrayList.get(ArrayList.java:437)
        at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
        at android.widget.ListView.dispatchDraw(ListView.java:3503)
        at android.view.View.draw(View.java:19126)
        at android.widget.AbsListView.draw(AbsListView.java:4317)
        at android.view.View.updateDisplayListIfDirty(View.java:18073)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
        at android.view.View.updateDisplayListIfDirty(View.java:18032)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:643)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:649)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:757)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2980)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2794)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2347)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)

  • 写回答

3条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-06-02 10:41
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月15日
  • 已采纳回答 6月7日
  • 创建了问题 6月2日

悬赏问题

  • ¥15 关于cpci总线的几个问题,有点迷糊
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护