最后更新:2013-01-16
23.在使用AndroidHttpClient時有如下錯誤:
01-16 22:18:39.222: E/AndroidHttpClient(26745): java.lang.IllegalStateException: AndroidHttpClient created and never closed
這個主要是因為沒有調(diào)用AndroidHttpClient的close()方法。它的close()方法如下:
1 2 3 4 5 6 7 8 9 10 | /**
* Release resources associated with this client. You must call this,
* or significant resources (sockets and memory) may be leaked.
*/
public void close() {
if (mLeakedException != null ) {
getConnectionManager().shutdown();
mLeakedException = null ;
}
}
|
在我們的使用中在finally語句塊中調(diào)用其close方法即可。
1 2 3 4 5 | finally {
if (httpClient != null ) {
httpClient.close();
}
}
|
最后更新:2012-11-12
23,在使用一個FrameLayout作為容器時,將一個Frament實例動態(tài)添加進(jìn)布局時,出現(xiàn)如下錯誤提示:
The specified child already has a parent. You must call removeView() on the child's parent first.
解決辦法就是:
在
1 2 3 4 5 6 7 8 | @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(DIALOG_FRAGMENT, "====(3)onCreateView========>" );
View view = inflater.inflate(R.layout.auth, container, false );
initViews(view);
return view;
}
|
中,使用,注意帶參數(shù)false
View view = inflater.inflate(R.layout.auth, container, false);
22.android:filterTouchesWhenObscured
view所在窗口被其它可見窗口遮住時,是否過濾觸摸事件。
結(jié)果因為有些應(yīng)用會在界面的上面加一層,導(dǎo)致軟件不可滑動。所以。
最后更新 2012-04-28
21.接下面的錯誤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 05 - 10 16 : 16 : 23.830 : W/dalvikvm( 15086 ): threadid= 1 : thread exiting with uncaught exception (group= 0x40a4b1f8 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): FATAL EXCEPTION: main
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): java.lang.IllegalStateException: Activity has been destroyed
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java: 1314 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java: 541 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java: 529 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.activity.BaseFragmentActivity.removeFragmentDialog(BaseFragmentActivity.java: 129 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.activity.BaseFragmentActivity.removeFragmentDialog(BaseFragmentActivity.java: 114 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.guilin.DiTouchClientActivity.access$ 1 (DiTouchClientActivity.java: 1 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.guilin.DiTouchClientActivity$ConnectServerTask.onPostExecute(DiTouchClientActivity.java: 299 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.guilin.DiTouchClientActivity$ConnectServerTask.onPostExecute(DiTouchClientActivity.java: 1 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.AsyncTask.finish(AsyncTask.java: 602 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.AsyncTask.access$ 600 (AsyncTask.java: 156 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java: 615 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.Handler.dispatchMessage(Handler.java: 99 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.Looper.loop(Looper.java: 137 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.app.ActivityThread.main(ActivityThread.java: 4424 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at java.lang.reflect.Method.invokeNative(Native Method)
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at java.lang.reflect.Method.invoke(Method.java: 511 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 784 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 551 )
05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at dalvik.system.NativeStart.main(Native Method)
|
在此有說是因為FragmentManager自己是在onCreate中初始化的所以要把fragment的操作放到后面去:
http:///questions/6938368/why-do-i-get-illegalstateexceptions-every-time-i-start-an-activity-that-uses-sup
20. 在debug帶有android.support.v4的包時,結(jié)果出現(xiàn)下面的錯誤:
05-10 15:47:09.850: E/AndroidRuntime(12744): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
比較完整的stack如下:
1 2 3 4 5 6 7 8 | 05 - 10 15 : 47 : 06.470 : W/dalvikvm( 12744 ): threadid= 1 : thread exiting with uncaught exception (group= 0x40a4b1f8 )
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): FATAL EXCEPTION: main
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java: 1299 )
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java: 1310 )
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java: 541 )
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java: 525 )
05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.DialogFragment.show(DialogFragment.java: 123 )
|
在此處有人也一個有點類似問題:
http:///questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-h
我看到上面有提到onSaveInstanceState(),于是就重寫了下這個.像上面說的不能調(diào)用super.我調(diào)用了,而且居然就tmd沒有錯誤提示了..
19.05-10 16:08:43.190: E/AndroidRuntime(14243): java.lang.IllegalStateException: addToBackStack() called after commit()
18.在使用android.support.v7.widget.GridLayout時出錯:
參考:http:///questions/10133078/assistance-please-setting-up-android-support-package-v7-for-eclipse-gridlayou
其中有兩個重要的特點:1.要將android-support-v7作為一個庫引用項目.
然后使用全名.然后,還是要將android-support-v7作為一個庫項目,來處理,命名空間等待.
17. android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
這個我自己的是在一個網(wǎng)絡(luò)出現(xiàn)異常,所以沒有按順序執(zhí)行close(),解決辦法是添加finally語句并執(zhí)行close()語句.
(2012-04-23)
16.04-23 14:50:08.344: E/AndroidRuntime(28034): Caused by: java.lang.IllegalStateException: Fragment cn.ditouch.client.activity.EditMenuxFragment did not create a view.
在使用Fragment中出現(xiàn)上面的錯誤,我是照著Google Android的文檔來做的.
錯誤的原因是我將FrameLayout看成了fragment想當(dāng)然了...
15. android.util.AndroidRuntimeException: requestFeature() must be called before adding content
在為AlertDialog中使用自定義布局時,在Builder中調(diào)用 了create()后不小心在使用了setContentView()
事實上也不應(yīng)該在使用setConentView了.因為如果先設(shè)置了這個會與后面添加產(chǎn)生沖突..
14.android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
原來及解決辦法:
導(dǎo)致報這個錯是在于new AlertDialog.Builder(mcontext),雖然這里的參數(shù)是AlertDialog.Builder(Context context)但我們不能使用getApplicationContext()獲得的Context,而必須使用Activity,因為只有一個Activity才能添加一個窗體。
Thanks to :http://blog.csdn.net/Vincent_czz/article/details/5777725
13. 在使用AlertDialog中,使用了如下代碼.
1 2 3 4 | return new AlertDialog.Builder( this )
.setTitle( "提醒!" )
.setMessage(args.getString( "MESSAGE" ))
.setCancelable( false )
|
但是,實際使用我發(fā)現(xiàn),如果我是在Activity中托管使用這個的話,在整個Acitivty生命周期中只創(chuàng)建一次,所以,args.getString()就只能得到第一次得到內(nèi)容.
解決辦法是使用onPrepareDialog(),現(xiàn)在知道為什么要這樣子設(shè)計了.
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Override
protected void onPrepareDialog( int id, Dialog dialog, Bundle args) {
AlertDialog aDialog = (AlertDialog) dialog;
switch (id) {
case DIALOG_FIRE_ALL_ORDER:
aDialog.setMessage(args.getCharSequence( "MESSAGE" ));
break ;
case DIALOG_DELETE_ORDER_NON_FIRED:
aDialog.setMessage(args.getString( "DISH_NAME" ));
default :
break ;
}
}
|
12.在ListView的適配器中出現(xiàn):java.lang.IndexOutOfBoundsException: Invalid index 32, size is 23
原因是我在getView()中使用了.
mCheckBoxList.add(position, holder.check);
解決辦法是,使用:mCheckBoxList.add(mCheckBoxList.size(), holder.check);
11.NetworkOnMainThreadException
當(dāng)我把targetSdkVersion設(shè)置為11時,我原來在UI線程執(zhí)行網(wǎng)絡(luò)操作的代碼
就會出現(xiàn)上面的異常,解決辦法是使用AsyncTask來網(wǎng)絡(luò)操作.或者使用其他的網(wǎng)絡(luò)庫如Volley,
10. close() was never explicitly called on database
04-12 17:19:34.020: E/SQLiteDatabase(2743): close() was never explicitly called on database '/data/data/xx.db'
這個在我使用完SQLiteOpenHelper的子類對象之后調(diào)用close()方法沒有報這個錯了.
Thanks to :http:///questions/4464892/android-error-close-was-never-explicitly-called-on-database
9.在使用SQLiteOpenHelper時出現(xiàn)這樣一個錯誤:Can't upgrade read-only database from version 0 to 1
04-12 16:59:11.230: E/AndroidRuntime(2119): android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: /data/data/databases/xx.db
從輸出的log中我發(fā)現(xiàn)現(xiàn)在這個錯誤的時候,同時前面會有一個sql語法錯誤.
解決辦法是根據(jù)提示,仔細(xì)檢查sql語句看是否有錯誤.
8.在UI線程中開了一個線程來初始化一個ViewFlipper.
導(dǎo)致下面的錯誤 :
04-10 14:08:36.356: E/AndroidRuntime(26716): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
解決辦法使用post方法將操作post過去 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ToastUtils.showLong( this , "正在加載樓層數(shù)據(jù)..." );
new Thread( new Runnable() {
@Override
public void run() {
for (String floorId : FLOOR_NUMS) {
final List<Table> tables = TableDao.findByFloorId(floorId);
mFloorFlipper.post( new Runnable() {
@Override
public void run() {
buildFloorView(tables);
}
});
}
}
}).start();
|
7.在使用LayoutInflater.inflate()時經(jīng)常被第地個參數(shù)給搞定了.我是在適配器上使用的:
1 2 3 | public View getView( int position, View convertView, ViewGroup parent) {
if (convertView != null ) return convertView;
View view = mInflater.inflate(R.layout.menu_item, parent);
|
錯誤信息如下:
04-02 02:49:06.322: E/AndroidRuntime(494): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
應(yīng)該改成下面這樣 :mInflater.inflate(R.layout.menu_item,parent,false);因為我們需要使用parent來幫助構(gòu)造LayoutParams,但不是要添加進(jìn)parent
6.在使用SharedPreferences的時候,使用了getPreferences().但是這個是只供本Acitivity使用的.
如果在本應(yīng)用的其它activity中共享這些設(shè)置,就要使用getSharedPreferences();
5.第四個錯誤說到,要在每一個ListView中的項中實現(xiàn),手勢滑動判斷,刪除功能.但是如果沒有設(shè)置背景圖片或者背景色的時候,當(dāng)手滑動區(qū)域超過一個ListView的項時,就會出現(xiàn)ListView項背景反色,很是讓人討厭,一開始我以為是因為,ListView項,獲得了焦點的原因,就嘗試在項中的GestureOverlayView中及在其中的TextView的onTouch方法中如果MotionEvent的action為down的話,就返回true,這個系統(tǒng)就不會處理高亮反色了,但是其實不是這樣的,到底是怎么樣的呢?
目前還不清楚,我的解決辦法是在GestureOverlayView中設(shè)置一張背景圖片.就OK了.就算這個ListView中各個項高亮反色了.
也不會影響.
錯誤設(shè)置代碼如下,處理高亮還是不要這樣用了-在這樣的場景下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | private static class IgnoreTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "Ignore touch" + v.getClass() + "," + event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true ;
default :
break ;
}
return false ;
}
}
|
4. 現(xiàn)在的一個需要是要在一個ListView的項中滑動時,就表示刪除其中一個項.
開始用GestureDetector但是,弄了很久還是沒有反應(yīng).所以作罷,關(guān)鍵在一個項中,我喜歡滑動的時候能夠有像用筆畫線一樣的效果.
ListView中的項對應(yīng)布局文件如下 :(名為:orderlist_item.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <? xml version = "1.0" encoding = "utf-8" ?>
< android.gesture.GestureOverlayView
xmlns:android = "http://schemas./apk/res/android"
android:id = "@+id/gestures"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:eventsInterceptionEnabled = "true"
android:background = "@drawable/bg_current_order_item"
android:orientation = "vertical" >
< TextView
android:id = "@+id/order_list_item_text"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:padding = "5dp"
android:text = "TextView01"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_centerHorizontal = "false"
>
</ TextView >
</ android.gesture.GestureOverlayView >
|
開始嘗試:這樣:
1 2 3 | GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener( this );
|
但是這樣會報空指針錯誤,就是取不到gestures.但是我?guī)蚢ndroid的示例項目也是這樣的.android資源中文章:
docs/resources/articles/gestures.
后來我想了一下,在android中的示例項目 中,這是一唯一的一個文件,但是在我的使用場景中這樣的一個文件在ListView中重復(fù)使用了,想起來這樣的一個ID也會在上面存在多個.于是用R.id.gestures是找不到了,就算用R.layout.orderlist_item也找不到.
于是我就在自己實現(xiàn)的BaseAdapter中的View來處理這樣的邏輯了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Override
public View getView( int position, View convertView, ViewGroup parent) {
View view = null ;
if (convertView == null ) {
view = mInflater.inflate(R.layout.orderlist_item, null );
TextView textView = (TextView) view
.findViewById(R.id.order_list_item_text);
Map<String, Object> map = (Map<String, Object>) mData
.get(position);
String itemText = (String) map.get(ITEM_TEXT);
textView.setText(itemText);
view.setTag(mData.get(position));
GestureOverlayView gestures = (GestureOverlayView) view;
gestures.addOnGesturePerformedListener(ClientStationActivity. this );
}
return view;
}
|
其中處理在主Activity中實現(xiàn)的OnGesturePerforedListener接口,其中處理方法 如下 :
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
if (predictions.size() < 1 )
return ;
if (predictions.get( 0 ).score < 0.27 )
return ;
String action = predictions.get( 0 ).name;
Log.i(TAG, "action:" + action + "," + predictions.get( 0 ).score);
Map<String, Object> map = (Map<String, Object>) overlay.getTag();
String itemText = (String) map.get(ITEM_TEXT);
// TODO 完成刪除
}
|
上面的代碼有一個要注意的地方就是.通過實際的手勢來判斷看你認(rèn)為的一個比較正確的手勢的score得分是多少.
3.
不能在android項目中運(yùn)行Java程序.
有時候想運(yùn)行測試一下一個簡單的Java方法,但是呢今天發(fā)現(xiàn)了這樣一個問題:
然后我就想通了.
報告錯誤如下 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:3494), pid=2260, tid=2492
# Error: ShouldNotReachHere()
#
# JRE version: 6.0_31-b05
# Java VM: Java HotSpot(TM) Client VM (20.6-b01 mixed mode windows-x86 )
# An error report file with more information is saved as:
# E:\workspace\demo\hs_err_pid2260.log
#
# If you would like to submit a bug report, please visit:
# http://java./webapps/bugreport/crash.jsp
#
|
1.
03-17 04:14:51.044: E/AndroidRuntime(390): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
說的是在RelativeLayout循環(huán)引用了吧。我相對你,你又相對我。哈哈。
2.
03-17 08:27:04.605: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.content.res.Resources$NotFoundException: String resource ID #0x7f060004
事實上我看到我項目的R文件上有這么一句:
1 | public static final int new_table= 0x7f060004 ;
|
而我引用資源的代碼如下:
1 2 3 4 | private static final int [] LABELS = new int [] { R.string.new_table,
R.string.find_bill, R.string.change_waiter,
R.string.reconnect_server, R.string.exit_system };
getString(LABELS[i])
|
上網(wǎng)暫時沒有查找到什么有用的,因為別人出現(xiàn)這個錯誤是因為在setText()的時候直接用了int型的值。
我呢?哎。
然后我在回學(xué)校路上一直在想,然后我想,突然,我想起了android查找資源的的解析方法.
因為我開始開發(fā)時只提供了values-zh的字符串值,而沒有提供values下的默認(rèn)字符串值.
因為模擬器默認(rèn)的語言環(huán)境是英文的,所以就不會去查找values-zh下的字符串資源.
所以才會導(dǎo)致上面的錯誤.
然后只要在values中添加對應(yīng)的字符串資源就OK了.
希望我的這個錯誤,能給大家在遇到這個錯誤時,提供一些解決問題的啟示.
在android的參考文檔 guide/topics/resources/providing-resources.html 可以了解更多.