Android笔记:常见错误问题及解决方法汇总

发布时间:2020-07-20 05:54:02 作者:glblong
来源:网络 阅读:8130

1.Android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()  

    E/AndroidRuntime(7200): Uncaught handler: thread Thread-8 exiting due to uncaught exception
    E/AndroidRuntime( 7200): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()


原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。


解决方法:

new Thread() {

public void run() { 

 Looper.prepare(); 

 mPst.startPushService(); 

 mPst.sendJson2Server(qJson);//上线发消息给server 

 Looper.loop(); 

 }

 }.start();

加上上面红色两行。



2.java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131362336, class android.widget.ListView) with Adapter(class com.manjay.housebox.slidemenu.SpecialListFragment$SpecialAdapter)]

java.lang.IllegalStateException: 
The content of the adapter has changed but ListView did not receive a notification. 
Make sure the content of your adapter is not modified from a background thread, 
but only from the UI thread. 
[in ListView(2131362336, class android.widget.ListView) with Adapter(class com.manjay.housebox.slidemenu.SpecialListFragment$SpecialAdapter)]

错误的大体意思是:你的adapter的内容变化了,但是你的ListView并不知情。请保证你adapter的数据在主线程中进行更改!


解决方法:

1、检查Thread,确定没有在Background thread中直接调用adapter,如果有,请移除相关代码到Handler中处理; 

2、尽量将数据放在adapter类中管理,不需要的时候清除信息(勤写clear()),及时用notifyDataSetChanged()刷新; 

3、在Activity或者Fragment合适的位置(onPause/onStop)要及时检查thread,有adapter数据处理相关的应马上停止; 

4、这个错误经常出现在Activity休眠起来之后,主要还是使用adapter不太小心造成的。如果实在找不到原因,在onPause()函数中停止所有的background thread,并且在onResume()函数最前面清空adapter中的数据,并且adapter.notifyDataSetChanged()。然后重新更新加载数据,这样一般可以解决问题。 


经过尝试,问题最终通过第二种方法解决了。

实现方法如下:

    class SpecialAdapter extends BaseAdapter
    {
        //将数据集合转移到适配器里
        private ArrayList<SpecialInfo> dataList;
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
            {
                LayoutInflater inflater = getActivity().getLayoutInflater();
                convertView = inflater.inflate(R.layout.speciallist_item, null);
            }
            
            ImageView iv_main = ViewHolder.get(convertView, R.id.speciallist_item_p_w_picpath);
            TextView tv_title = ViewHolder.get(convertView, R.id.speciallist_item_title);
            
            SpecialInfo data = dataList.get(position);
            if (position == 0)
            {
                layoutView.setVisibility(View.GONE);
            }
            else
            {
                layoutView.setVisibility(View.VISIBLE);
            }
            
            Bitmap bm = BitmapFactory.decodeResource(getActivity().getResources(), data.icon);
            iv_main.setImageBitmap(bm);
            tv_title.setText(data.title);

            return convertView;
        }
        
        @Override
        public int getCount()
        {
            return dataList == null ? 0 : dataList.size();
        }
        
        @Override
        public Object getItem(int position)
        {
            return null;
        }
        
        @Override
        public long getItemId(int position)
        {
            return 0;
        }
        
        //更新数据
        public void setDataList(ArrayList<SpecialInfo> list)
        {
            if (list != null)
            {
                dataList = (ArrayList<SpecialInfo>) list.clone();
                notifyDataSetChanged();
            }
        }
        
        //释放数据
        public void clearDataList()
        {
            if (dataList != null)
            {
                dataList.clear();
            }
            notifyDataSetChanged();
        }
        
    }


注:

1.将所有数据“完全”保存在adapter内部,即使有外部数据进入,也会用.clone()重新生成副本,保证了数据完全是由adapter维护的。

2.保证所有setDeviceList()/clearDeviceList()是从主线程里调用的。


参考资料:http://blog.csdn.net/ueryueryuery/article/details/20607845




3.android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.


问题所在:UI操作不能在子线程(非UI线程)操作.




4.使用proguardgui混淆器对jar包进行混淆,出现EXCEPTION FROM SIMULATION错误

详见:Android项目:proguard混淆之常见问题及解决方法汇总



5.so文件所在目录问题

Caused by: java.lang.UnsatisfiedLinkError: Couldn't load amapv301 from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.manjay.housebox-1.apk,libraryPath=/data/app-lib/com.manjay.housebox-1]: findLibrary returned null

错误详情:

Android笔记:常见错误问题及解决方法汇总


解决方法:

报错的是红色框的so文件,将红色框的so文件复制一份到蓝色框目录里,或者新建armeabi-v7a目录并将so文件复制进去。

Android笔记:常见错误问题及解决方法汇总



6.加载fragment时报错:IllegalStateException: Can not perform this action after onSaveInstanceState

是在使用FragmentTransition的 commit方法添加一个Fragment的时候出现的。commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。



7.Export的时候遇到xxx is not translated in yyy, zzz的问题。

例如说"auto_exit" is not translated in zh, zh_CN.

这是因为Android SDK Tool 將 ANDROID_LINT_COMPLETE_REGIONS 改为了需要检查。


临时解决方法:

Eclipse > Preference > Android > Lint Error Checking的Correctness: Messages > MissingTranslate

将 Severity 从 Fetal 改为 Warming



8.android FAILED Binder Transaction 问题

    Intent传输的bytes不能超过40k。优其在Intent 中传递图片时,要限制图片小 40K.

参考资料:

1.http://blog.csdn.net/glony/article/details/7596430

2.http://stackoverflow.com/questions/3528735/failed-binder-transaction



9.com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 157.

  使用Gson解析时出现的问题,问题缘由是被解析的对象类的属性类型错误所致。

  比如数组(JSONArray)类型的属性,写法应该是:List<类型> 属性名;


10.Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

很多人使用startActivity时候,会碰到如下异常:

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

原因:Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。


解决办法:加一个flag。


intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

这样就可以再新的task里面启动这个Activity了。





注:

1.混淆相关的问题处理转至:Android项目:proguard混淆之常见问题及解决方法汇总





推荐阅读:
  1. 怎么在MacOS中安装Tclip扩展
  2. 使用php怎么编写一个E-mail验证类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

runtimeexception android notifydatasetchanged

上一篇:关于C#调用大漠插件时提示“{26037A0E-7CBD-4FFF-9C63-56F2D0770214} 的组件时失败”的解决办法

下一篇:Thinkphp源码分析之类的自动加载

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》