溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android筆記:常見錯誤問題及解決方法匯總

發布時間:2020-07-20 05:54:02 來源:網絡 閱讀:8240 作者:glblong 欄目:移動開發

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混淆之常見問題及解決方法匯總





向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女