您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页尚学堂android开发常用错误集

尚学堂android开发常用错误集

来源:保捱科技网


23.在使用AndroidHttpClient时有如下错误: 01-16 22:18:39.222: E/AndroidHttpClient(26745): java.lang.IllegalStateException: AndroidHttpClient created and never closed 这个主要是因为没有调用AndroidHttpClient的close()方法。它的close()方法如下: ?

1 /**

2 * Release resources associated with this client. You must call this, 3 * or significant resources (sockets and memory) may be leaked. 4 */

5 public void close() {

6 if (mLeakedException != null) {

7 getConnectionManager().shutdown(); 8 mLeakedException = null; 9 } 10 }

在我们的使用中在finally语句块中调用其close方法即可。 ?

1 finally {

2 if(httpClient != null) { 3 httpClient.close(); 4 } 5 }

最后更新:2012-11-12

23,在使用一个FrameLayout作为容器时,将一个Frament实例动态添加进布局时,出现如下错误提示:

The specified child already has a parent. You must call removeView() on the child's parent first. 解决办法就是: 在 ?

1 @Override

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); 6 initViews(view); 7 return view; 8 }

中,使用,注意带参数false

View view = inflater.inflate(R.layout.auth, container, false); 22.android:filterTouchesWhenObscured

view所在窗口被其它可见窗口遮住时,是否过滤触摸事件。

结果因为有些应用会在界面的上面加一层,导致软件不可滑动。所以。

最后更新 2012-04-28

21.接下面的错误: ? 1 2 3 4 5 6 7

05-10 16:16:23.830: W/dalvikvm(15086): threadid=1: thread exiting with uncaught exception (g8

05-10 16:16:23.860: E/AndroidRuntime(15086): FATAL EXCEPTION: main 9

05-10 16:16:23.860: E/AndroidRuntime(15086): java.lang.IllegalStateException: Activity has bee1

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.support.v4.app.FragmentManager0

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.support.v4.app.BackStackRecord.c1

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.support.v4.app.BackStackRecord.c1

05-10 16:16:23.860: E/AndroidRuntime(15086): at cn.ditouch.client.activity.BaseFragmentAct1

05-10 16:16:23.860: E/AndroidRuntime(15086): at cn.ditouch.client.activity.BaseFragmentAct2

05-10 16:16:23.860: E/AndroidRuntime(15086): at cn.ditouch.client.guilin.DiTouchClientActiv1

05-10 16:16:23.860: E/AndroidRuntime(15086): at cn.ditouch.client.guilin.DiTouchClientActiv3

05-10 16:16:23.860: E/AndroidRuntime(15086): at cn.ditouch.client.guilin.DiTouchClientActiv1

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.os.AsyncTask.finish(AsyncTask.jav4

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.os.AsyncTask.access$600(AsyncTa1

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.os.AsyncTask$InternalHandler.han5

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.os.Handler.dispatchMessage(Hand1

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.os.Looper.loop(Looper.java:137) 6

05-10 16:16:23.860: E/AndroidRuntime(15086): at android.app.ActivityThread.main(ActivityT1

05-10 16:16:23.860: E/AndroidRuntime(15086): at java.lang.reflect.Method.invokeNative(Nati7

05-10 16:16:23.860: E/AndroidRuntime(15086): at java.lang.reflect.Method.invoke(Method.jav1

05-10 16:16:23.860: E/AndroidRuntime(15086): at com.android.internal.os.ZygoteInit$Metho8

05-10 16:16:23.860: E/AndroidRuntime(15086): at com.android.internal.os.ZygoteInit.main(Z1

05-10 16:16:23.860: E/AndroidRuntime(15086): at dalvik.system.NativeStart.main(Native Met9 20 21 22

在此有说是因为FragmentManager自己是在onCreate中初始化的所以要把fragment的操作放到后面去:

http://stackoverflow.com/questions/6938368/why-do-i-get-illegalstateexceptio

ns-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 (gr2 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 4 05-10 15:47:09.850: E/AndroidRuntime(12744): at android.support.v4.app.FragmentManagerI5 05-10 15:47:09.850: E/AndroidRuntime(12744): at android.support.v4.app.FragmentManagerI6 05-10 15:47:09.850: E/AndroidRuntime(12744): at android.support.v4.app.BackStackRecord.co7 05-10 15:47:09.850: E/AndroidRuntime(12744): at android.support.v4.app.BackStackRecord.co8 05-10 15:47:09.850: E/AndroidRuntime(12744): at android.support.v4.app.DialogFragment.sh在此处有人也一个有点类似问题:

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) 2 .setTitle(\"提醒!\")

3 .setMessage(args.getString(\"MESSAGE\")) 4 .setCancelable(false)

但是,实际使用我发现,如果我是在Activity中托管使用这个的话,在整个Acitivty生命周期中只创建一次,所以,args.getString()就只能得到第一次得到内容. 解决办法是使用onPrepareDialog(),现在知道为什么要这样子设计了. ?

1 @Override

2 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { 3 AlertDialog aDialog = (AlertDialog) dialog; 4 switch (id) {

5 case DIALOG_FIRE_ALL_ORDER:

6 aDialog.setMessage(args.getCharSequence(\"MESSAGE\")); 7 break;

8 case DIALOG_DELETE_ORDER_NON_FIRED:

9 aDialog.setMessage(args.getString(\"DISH_NAME\")); 10 default: 11 break; 12 } 13 }

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来网络操作.或者使用其他的网络库如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的子类对象之后调用close()方法没有报这个错了. Thanks to :http://stackoverflow.com/questions/442/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过去 : ?

1 ToastUtils.showLong(this, \"正在加载楼层数据...\"); 2 new Thread(new Runnable() { 3 @Override

4 public void run() {

5 for (String floorId : FLOOR_NUMS) {

6 final List

tables = TableDao.findByFloorId(floorId); 7 mFloorFlipper.post(new Runnable() { 8 @Override

9 public void run() {

10 buildFloorView(tables); 11 } 12 }); 13 } 14 }

15 }).start();

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

应该改成下面这样 :mInflater.inflate(R.layout.menu_item,parent,false);因为我们需要使用parent来帮助构造LayoutParams,但不是要添加进parent

6.在使用SharedPreferences的时候,使用了getPreferences().但是这个是只供本Acitivity使用的.

如果在本应用的其它activity享这些设置,就要使用getSharedPreferences();

5.第四个错误说到,要在每一个ListView中的项中实现,手势滑动判断,删除功能.但是如果没有设置背景图片或者背景色的时候,当手滑动区域超过一个ListView的项时,就会出现ListView项背景反色,很是让人讨厌,一开始我以为是因为,ListView项,获得了焦点的原因,就尝试在项中的GestureOverlayView中及在其中的TextView的onTouch方法中如果MotionEvent的action为down的话,就返回true,这个系统就不会处理高亮反色了,但是其实不是这样的,到底是怎么样的呢?

目前还不清楚,我的解决办法是在GestureOverlayView中设置一张背景图片.就OK了.就算这个ListView中各个项高亮反色了. 也不会影响.

错误设置代码如下,处理高亮还是不要这样用了-在这样的场景下: ?

1 private static class IgnoreTouchListener implements View.OnTouchListener { 2 @Override

3 public boolean onTouch(View v, MotionEvent event) {

4 Log.i(TAG, \"Ignore touch\" + v.getClass() + \5 switch (event.getAction()) {

6 case MotionEvent.ACTION_DOWN: 7 return true; 8 default: 9 break; 10 }

11 return false; 12 } 13 }

4. 现在的一个需要是要在一个ListView的项中滑动时,就表示删除其中一个项. 开始用GestureDetector但是,弄了很久还是没有反应.所以作罢,关键在一个项中,我喜欢滑动的时候能够有像用笔画线一样的效果.

ListView中的项对应布局文件如下 :(名为:orderlist_item.xml) ?

1 2 3 xmlns:android=\"http://schemas.android.com/apk/res/android\" 4 android:id=\"@+id/gestures\"

5 android:layout_width=\"fill_parent\" 6 android:layout_height=\"fill_parent\"

7 android:eventsInterceptionEnabled=\"true\"

8 android:background=\"@drawable/bg_current_order_item\" 9 android:orientation=\"vertical\"> 10 11 android:id=\"@+id/order_list_item_text\" 12 android:layout_width=\"wrap_content\" 13 android:layout_height=\"wrap_content\" 14 android:padding=\"5dp\" 15 android:text=\"TextView01\"

16 android:textAppearance=\"?android:attr/textAppearanceLarge\" 17 android:layout_centerHorizontal=\"false\" 18 >

19

20 开始尝试:这样: ?

1 GestureOverlayView gestures = (GestureOverlayView)findViewById(R.id.gestures); 2

3 gestures.addOnGesturePerformedListener(this);

但是这样会报空指针错误,就是取不到gestures.但是我帮android的示例项目也是这样的.android资源中文章: docs/resources/articles/gestures.

后来我想了一下,在android中的示例项目 中,这是一唯一的一个文件,但是在我的使用场景中这样的一个文件在ListView中重复使用了,想起来这样的一个ID也会在上面存在多个.于是用R.id.gestures是找不到了,就算用R.layout.orderlist_item也找不到.

于是我就在自己实现的BaseAdapter中的View来处理这样的逻辑了: ?

1 @Override

2 public View getView(int position, View convertView, ViewGroup parent) { 3 View view = null;

4 if (convertView == null) {

5 view = mInflater.inflate(R.layout.orderlist_item, null); 6 TextView textView = (TextView) view

7 .findViewById(R.id.order_list_item_text);

8 Map map = (Map) mData 9 .get(position);

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); 15 16 }

17 return view; 18 }

其中处理在主Activity中实现的OnGesturePerforedListener接口,其中处理方法 如下 : ?

1 @Override

2 public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 3 ArrayList predictions = mLibrary.recognize(gesture); 4 if (predictions.size() < 1) 5 return;

6 if (predictions.get(0).score < 0.27) 7 return;

8 String action = predictions.get(0).name;

9 Log.i(TAG, \"action:\" + action + \

10 Map map = (Map) overlay.getTag(); 11 String itemText = (String) map.get(ITEM_TEXT); 12 // TODO 完成删除 13 }

上面的代码有一个要注意的地方就是.通过实际的手势来判断看你认为的一个比较正确的手势的score得分是多少. 3.

不能在android项目中运行Java程序.

有时候想运行测试一下一个简单的Java方法,但是呢今天发现了这样一个问题: 然后我就想通了. 报告错误如下 : ?

1 #

2 # A fatal error has been detected by the Java Runtime Environment: 3 #

4 # Internal Error (classFileParser.cpp:3494), pid=2260, tid=2492 5 # Error: ShouldNotReachHere() 6 #

7 # JRE version: 6.0_31-b05

8 # Java VM: Java HotSpot(TM) Client VM (20.6-b01 mixed mode windows-x86 ) 9 # An error report file with more information is saved as: 10 # E:\\workspace\\demo\\hs_err_pid2260.log 11 #

12 # If you would like to submit a bug report, please visit: 13 # http://java.sun.com/webapps/bugreport/crash.jsp 14 # 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 }; 4 getString(LABELS[i])

上网暂时没有查找到什么有用的,因为别人出现这个错误是因为在setText()的时候直接用了int型的值。 我呢?哎。

然后我在回学校路上一直在想,然后我想,突然,我想起了android查找资源的的解析方法.

因为我开始开发时只提供了values-zh的字符串值,而没有提供values下的默认字符串值.

因为模拟器默认的语言环境是英文的,所以就不会去查找values-zh下的字符串资源.

所以才会导致上面的错误.

然后只要在values中添加对应的字符串资源就OK了.

希望我的这个错误,能给大家在遇到这个错误时,提供一些解决问题的启示.

在android的参考文档 guide/topics/resources/providing-resources.html 可以了解更多.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务