01-16 22:18:39.222: E/AndroidHttpClient(26745): java.lang.IllegalStateException: AndroidHttpClient created and never closed
这个主要是因为没有调用AndroidHttpClient的close()方法。它的close()方法如下:
02 | * Release resources associated with this client. You must call this, |
03 | * or significant resources (sockets and memory) may be leaked. |
06 | if (mLeakedException != null ) { |
07 | getConnectionManager().shutdown(); |
08 | mLeakedException = null ; |
在我们的使用中在finally语句块中调用其close方法即可。
2 | if (httpClient != null ) { |
最后更新:2012-11-12
23,在使用一个FrameLayout作为容器时,将一个Frament实例动态添加进布局时,出现如下错误提示:
The specified child already has a parent. You must call removeView() on the child's parent first.
解决办法就是:
在
2 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
3 | Bundle savedInstanceState) { |
4 | Log.i(DIALOG_FRAGMENT, "====(3)onCreateView========>" ); |
5 | View view = inflater.inflate(R.layout.auth, container, false ); |
中,使用,注意带参数false
View view = inflater.inflate(R.layout.auth, Container, false);
22.Android:filterTouchesWhenObscured
view所在窗口被其它可见窗口遮住时,是否过滤触摸事件。
结果因为有些应用会在界面的上面加一层,导致软件不可滑动。所以。
最后更新 2012-04-28
21.接下面的错误:
01 | 05 - 10 16 : 16 : 23.830 : W/dalvikvm( 15086 ): threadid= 1 : thread exiting with uncaught exception (group= 0x40a4b1f8 ) |
02 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): FATAL EXCEPTION: main |
03 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): java.lang.IllegalStateException: Activity has been destroyed |
04 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java: 1314 ) |
05 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java: 541 ) |
06 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java: 529 ) |
07 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.activity.BaseFragmentActivity.removeFragmentDialog(BaseFragmentActivity.java: 129 ) |
08 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.activity.BaseFragmentActivity.removeFragmentDialog(BaseFragmentActivity.java: 114 ) |
09 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.guilin.DiTouchClientActivity.access$ 1 (DiTouchClientActivity.java: 1 ) |
10 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.guilin.DiTouchClientActivity$ConnectServerTask.onPostExecute(DiTouchClientActivity.java: 299 ) |
11 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at cn.ditouch.client.guilin.DiTouchClientActivity$ConnectServerTask.onPostExecute(DiTouchClientActivity.java: 1 ) |
12 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.AsyncTask.finish(AsyncTask.java: 602 ) |
13 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.AsyncTask.access$ 600 (AsyncTask.java: 156 ) |
14 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java: 615 ) |
15 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.Handler.dispatchMessage(Handler.java: 99 ) |
16 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.os.Looper.loop(Looper.java: 137 ) |
17 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at android.app.ActivityThread.main(ActivityThread.java: 4424 ) |
18 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at java.lang.reflect.Method.invokeNative(Native Method) |
19 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at java.lang.reflect.Method.invoke(Method.java: 511 ) |
20 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 784 ) |
21 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 551 ) |
22 | 05 - 10 16 : 16 : 23.860 : E/AndroidRuntime( 15086 ): at dalvik.system.NativeStart.main(Native Method) |
在此有说是因为FragmentManager自己是在onCreate中初始化的所以要把fragment的操作放到后面去:
http://stackoverflow.com/questions/6938368/why-do-i-get-illegalstateexceptions-every-time-i-start-an-activity-that-uses-sup
20. 在debug带有android.support.v4的包时,结果出现下面的错误:
05-10 15:47:09.850: E/AndroidRuntime(12744): Java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
比较完整的stack如下:
1 | 05 - 10 15 : 47 : 06.470 : W/dalvikvm( 12744 ): threadid= 1 : thread exiting with uncaught exception (group= 0x40a4b1f8 ) |
2 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): FATAL EXCEPTION: main |
3 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState |
4 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java: 1299 ) |
5 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java: 1310 ) |
6 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java: 541 ) |
7 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java: 525 ) |
8 | 05 - 10 15 : 47 : 09.850 : E/AndroidRuntime( 12744 ): at android.support.v4.app.DialogFragment.show(DialogFragment.java: 123 ) |
在此处有人也一个有点类似问题:
http://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-h
我看到上面有提到onSaveInstanceState(),于是就重写了下这个.像上面说的不能调用super.我调用了,而且居然就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://stackoverflow.com/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
这个我自己的是在一个网络出现异常,所以没有按顺序执行close(),解决办法是添加finally语句并执行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中出现上面的错误,我是照着Google Android的文档来做的.
错误的原因是我将FrameLayout看成了fragment想当然了...
15. android.util.AndroidRuntimeException: requestFeature() must be called before adding content
在为AlertDialog中使用自定义布局时,在Builder中调用 了create()后不小心在使用了setContentView()
事实上也不应该在使用setConentView了.因为如果先设置了这个会与后面添加产生冲突..
14.android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
原来及解决办法:
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。
Thanks to :http://blog.csdn.NET/Vincent_czz/article/details/5777725
13. 在使用AlertDialog中,使用了如下代码.
1 | return new AlertDialog.Builder( this ) |
3 | .setMessage(args.getString( "MESSAGE" )) |
但是,实际使用我发现,如果我是在Activity中托管使用这个的话,在整个Acitivty生命周期中只创建一次,所以,args.getString()就只能得到第一次得到内容.
解决办法是使用onPrepareDialog(),现在知道为什么要这样子设计了.
02 | protected void onPrepareDialog( int id, Dialog dialog, Bundle args) { |
03 | AlertDialog aDialog = (AlertDialog) dialog; |
05 | case DIALOG_FIRE_ALL_ORDER: |
06 | aDialog.setMessage(args.getCharSequence( "MESSAGE" )); |
08 | case DIALOG_DELETE_ORDER_NON_FIRED: |
09 | aDialog.setMessage(args.getString( "DISH_NAME" )); |
12.在ListView的适配器中出现:java.lang.IndexOutOfBoundsException: Invalid index 32, size is 23
原因是我在getView()中使用了.
mCheckBoxList.add(position, holder.check);
解决办法是,使用:mCheckBoxList.add(mCheckBoxList.size(), holder.check);
11.NetworkOnMainThreadException
当我把targetSdkVersion设置为11时,我原来在UI线程执行网络操作的代码
就会出现上面的异常,解决办法是使用AsyncTask来网络操作.
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的子类对象之后调用close()方法没有报这个错了.
Thanks to :http://stackoverflow.com/questions/4464892/android-error-close-was-never-explicitly-called-on-database
9.在使用SQLiteOpenHelper时出现这样一个错误: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中我发现现在这个错误的时候,同时前面会有一个sql语法错误.
解决办法是根据提示,仔细检查sql语句看是否有错误.
8.在UI线程中开了一个线程来初始化一个ViewFlipper.
导致下面的错误 :
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过去 :
01 | ToastUtils.showLong( this , "正在加载楼层数据..." ); |
02 | new Thread( new Runnable() { |
05 | for (String floorId : FLOOR_NUMS) { |
06 | final List<Table> tables = TableDao.findByFloorId(floorId); |
07 | mFloorFlipper.post( new Runnable() { |
10 | buildFloorView(tables); |
7.在使用LayoutInflater.inflate()时经常被第地个参数给搞定了.我是在适配器上使用的:
1 | public View getView( int position, View convertView, ViewGroup parent) { |
2 | if (convertView != null ) return convertView; |
3 | 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
应该将parent换成null.
6.在使用SharedPreferences的时候,使用了getPreferences().但是这个是只供本Acitivity使用的.
如果在本应用的其它activity中共享这些设置,就要使用getSharedPreferences();
5.第四个错误说到,要在每一个ListView中的项中实现,手势滑动判断,删除功能.但是如果没有设置背景图片或者背景色的时候,当手滑动区域超过一个ListView的项时,就会出现ListView项背景反色,很是让人讨厌,一开始我以为是因为,ListView项,获得了焦点的原因,就尝试在项中的GestureOverlayView中及在其中的TextView的onTouch方法中如果MotionEvent的action为down的话,就返回true,这个系统就不会处理高亮反色了,但是其实不是这样的,到底是怎么样的呢?
目前还不清楚,我的解决办法是在GestureOverlayView中设置一张背景图片.就OK了.就算这个ListView中各个项高亮反色了.
也不会影响.
错误设置代码如下,处理高亮还是不要这样用了-在这样的场景下:
01 | private static class IgnoreTouchListener implements View.OnTouchListener { |
03 | public boolean onTouch(View v, MotionEvent event) { |
04 | Log.i(TAG, "Ignore touch" + v.getClass() + "," + event.getAction()); |
05 | switch (event.getAction()) { |
06 | case MotionEvent.ACTION_DOWN: |
4. 现在的一个需要是要在一个ListView的项中滑动时,就表示删除其中一个项.
开始用GestureDetector但是,弄了很久还是没有反应.所以作罢,关键在一个项中,我喜欢滑动的时候能够有像用笔画线一样的效果.
ListView中的项对应布局文件如下 :(名为:orderlist_item.xml)
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < android.gesture.GestureOverlayView |
03 | xmlns:android = "http://schemas.android.com/apk/res/android" |
04 | android:id = "@+id/gestures" |
05 | android:layout_width = "fill_parent" |
06 | android:layout_height = "fill_parent" |
07 | android:eventsInterceptionEnabled = "true" |
08 | android:background = "@drawable/bg_current_order_item" |
09 | android:orientation = "vertical" > |
11 | android:id = "@+id/order_list_item_text" |
12 | android:layout_width = "wrap_content" |
13 | android:layout_height = "wrap_content" |
15 | android:text = "TextView01" |
16 | android:textAppearance = "?android:attr/textAppearanceLarge" |
17 | android:layout_centerHorizontal = "false" |
20 | </ android.gesture.GestureOverlayView > |
开始尝试:这样:
1 | GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures); |
3 | gestures.addOnGesturePerformedListener( this ); |
但是这样会报空指针错误,就是取不到gestures.但是我帮android的示例项目也是这样的.android资源中文章:
docs/resources/articles/gestures.
后来我想了一下,在android中的示例项目 中,这是一唯一的一个文件,但是在我的使用场景中这样的一个文件在ListView中重复使用了,想起来这样的一个ID也会在上面存在多个.于是用R.id.gestures是找不到了,就算用R.layout.orderlist_item也找不到.
于是我就在自己实现的BaseAdapter中的View来处理这样的逻辑了:
02 | public View getView( int position, View convertView, ViewGroup parent) { |
04 | if (convertView == null ) { |
05 | view = mInflater.inflate(R.layout.orderlist_item, null ); |
06 | TextView textView = (TextView) view |
07 | .findViewById(R.id.order_list_item_text); |
08 | Map<String, Object> map = (Map<String, Object>) mData |
10 | String itemText = (String) map.get(ITEM_TEXT); |
11 | textView.setText(itemText); |
12 | view.setTag(mData.get(position)); |
13 | GestureOverlayView gestures = (GestureOverlayView) view; |
14 | gestures.addOnGesturePerformedListener(ClientStationActivity. this ); |
其中处理在主Activity中实现的OnGesturePerforedListener接口,其中处理方法 如下 :
02 | public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { |
03 | ArrayList<Prediction> predictions = mLibrary.recognize(gesture); |
04 | if (predictions.size() < 1 ) |
06 | if (predictions.get( 0 ).score < 0.27 ) |
08 | String action = predictions.get( 0 ).name; |
09 | Log.i(TAG, "action:" + action + "," + predictions.get( 0 ).score); |
10 | Map<String, Object> map = (Map<String, Object>) overlay.getTag(); |
11 | String itemText = (String) map.get(ITEM_TEXT); |
上面的代码有一个要注意的地方就是.通过实际的手势来判断看你认为的一个比较正确的手势的score得分是多少.
3.
不能在android项目中运行Java程序.
有时候想运行测试一下一个简单的Java方法,但是呢今天发现了这样一个问题:
然后我就想通了.
报告错误如下 :
1.
03-17 04:14:51.044: E/AndroidRuntime(390): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
说的是在RelativeLayout循环引用了吧。我相对你,你又相对我。哈哈。
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 | private static final int [] LABELS = new int [] { R.string.new_table, |
2 | R.string.find_bill, R.string.change_waiter, |
3 | R.string.reconnect_server, R.string.exit_system }; |
上网暂时没有查找到什么有用的,因为别人出现这个错误是因为在setText()的时候直接用了int型的值。
我呢?哎。
然后我在回学校路上一直在想,然后我想,突然,我想起了android查找资源的的解析方法.
因为我开始开发时只提供了values-zh的字符串值,而没有提供values下的默认字符串值.
因为模拟器默认的语言环境是英文的,所以就不会去查找values-zh下的字符串资源.
所以才会导致上面的错误.
然后只要在values中添加对应的字符串资源就OK了.
希望我的这个错误,能给大家在遇到这个错误时,提供一些解决问题的启示.
在android的参考文档 guide/topics/resources/providing-resources.html 可以了解更多