Android-调用finish()方法会先调用onPause(),然后在onStop/Destroy之前调用SimpleAdapter的getView()方法,导致崩溃
创始人
2024-08-19 18:00:03
0

这个问题通常发生在使用SimpleAdapter时,因为当调用finish()方法时会触发ListView重新绘制,在这个过程中,SimpleAdapter的getView()方法可能会在onStop或onDestroy之前被调用,导致崩溃。

一种解决方法是在调用finish()方法之前,先调用ListView的setAdapter(null)方法,以清除Adapter并防止调用getView()方法。示例代码如下:

ListView listView = findViewById(R.id.list_view);
listView.setAdapter(null);
finish();

还有一种解决方法是继承SimpleAdapter并覆盖其getView()方法,在方法中添加必要的检查以避免崩溃。示例代码如下:

public class MyAdapter extends SimpleAdapter {
    public MyAdapter(Context context, List> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            return super.getView(position, convertView, parent);
        } catch (IllegalStateException e) {
            // Ignore this exception
            return new View(getContext());
        }
    }
}

使用这个自定义Adapter时,需要将SimpleAdapter替换为MyAdapter,如下所示:

ListView listView = findViewById(R.id.list_view);
MyAdapter adapter = new MyAdapter(this, data, R.layout.item_layout, from, to);
listView.setAdapter(adapter);

这样,在调用finish()方法之后,SimpleAdapter的getView()方法就不会再被调用了,也就避免了崩溃。

相关内容

热门资讯

重大来袭!微乐小程序麻将自建房... 重大来袭!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)脚本运微乐小程...
玩家必看科普!微乐自建房辅助工... 玩家必看科普!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)技巧1)微乐家乡麻将自...
必看攻略!微乐家乡麻将自建房辅... 必看攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)脚本小薇(辅助器软...
科技新动态!微乐小程序麻将自建... 科技新动态!微乐小程序麻将自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)方法1)微乐小程序...
玩家必看分享!微乐自建房辅助工... 玩家必看分享!微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)技巧亲,关键说明,微乐自建房辅...
重大推荐!微乐家乡麻将自建房辅... 重大推荐!微乐家乡麻将自建房辅助app,微乐小程序免费黑科技(开挂自建房)app1、点击下载安装,微...
科普攻略!微乐家乡麻将自建房辅... 科普攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)脚本1、不需要AI...
揭秘几款!微乐微信自建房辅助工... 揭秘几款!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)app1、进入游戏-大厅左侧...
今日头条!微乐自建房辅助工具神... 今日头条!微乐自建房辅助工具神器,微乐家乡麻将自建房辅助app(开挂自建房)工具1、首先打开微乐家乡...
玩家必备攻略!微乐自建房辅助工... 玩家必备攻略!微乐自建房辅助工具免费app,微乐小程序免费黑科技(开挂自建房)脚本1、许多玩家不知道...