android开发中出现的错误集(不断更新)

27、android4.4之后和之前读取图片时的uri,路径有所变化,导致运行时获取不到图片。点击打开链接

http://www.bkjia.com/Androidjc/884759.html   http://blog.csdn.net/zzf112/article/details/37928305

1 Uri is:         content://com.android.providers.media.documents/document/image%3A18838
2 Uri.getPath is :/document/image:18838
3 对应的图片真实路径:/storage/emulated/0/Pictures/Screenshots/Screenshot_2014-09-22-21-40-53.png
1 Uri is:         content://media/external/images/media/18822
2 Uri.getPath is :/external/images/media/18822
3 对应的图片真实路径:/storage/emulated/0/Download/20130224235013.jpg
4.4之前的uri:

1 Uri is:         content://media/external/images/media/14046
2 Uri.getPath is :/external/images/media/14046
3 对应的图片真实路径:/storage/emulated/0/DCIM/Camera/20130224235013.jpg

26.在listview多布局中出现  java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 

原因:getItemViewType在返回布局类型是范围是0到getViewTypeCount-1. 源码注释:表示视图类型的整数。如果一个视图共享相同的类型
可以转换到其他{@链接# getview }。注:整数必须在范围0至{@链接# getviewtypecount } - 1。{“链接# ignore_item_view_type }可以
还将返回。

25.This Android SDK requires Android Developer Toolkit version 20.0.0 or above点击打开链接

windows 下面安装Android虚拟机,有时候选择更新SDK后,在Eclipse preference里指向android_sdk_windows_x86时。会出现诸如This AndroidSDK requires  Android Developer Toolkit version 20.0.0or above这样的提示,而且虚拟机无法打开的情况,这是由于选择更新后,在android_sdk_windows/tools/lib下的 plugin.prop文件里被变更为
# begin plugin.prop
plugin.version=20.0.0
# end plugin.prop

24.activity管理fragment嵌套fragment出现问题

转自:http://www.tuicool.com/articles/2eM32a

自从Android3.0引入了Fragment之后,使用Activity去嵌套一些Fragment的做法也变得更加流行,这确实是Fragment带来的一些优点,比如说:Fragment可以使你能够将activity分离成多个可重用的组件,每个都有它自己的生命周期和UI,更重要的是Fragment解决了Activity间的切换不流畅,实现了一种轻量及的切换,但是在官方提供的android.support.v4包中,Fragment还是或多或少的存在一些BUG,今天就与大家分享一下这些BUG和解决方法。

Case 1:当使用Fragment去嵌套另外一些子Fragment的时候,我们需要去管理子Fragment,这时候需要调用ChildFragmentManager去管理这些子Fragment,由此可能产生的Exception主要是: 
java.lang.IllegalStateException: No activity

首先我们来分析一下Exception出现的原因: 
通过DEBUG发现,当第一次从一个Activity启动Fragment,然后再去启动子Fragment的时候,存在指向Activity的变量,但当退出这些Fragment之后回到Activity,然后再进入Fragment的时候,这个变量变成null,这就很容易明了为什么抛出的异常是No activity

这个Exception是由什么原因造成的呢?如果想知道造成异常的原因,那就必须去看Fragment的相关代码,发现Fragment在detached之后都会被reset掉,但是它并没有对ChildFragmentManager做reset,所以会造成ChildFragmentManager的状态错误。

找到异常出现的原因后就可以很容易的去解决问题了,我们需要在Fragment被detached的时候去重置ChildFragmentManager,即:

<span class="nd">@Override</span>
<span class="indent">  </span><span class="kd"><span class="keyword" style="font-weight: bold;">public</span></span> <span class="kt"><span class="keyword" style="font-weight: bold;">void</span></span> <span class="nf">onDetach</span><span class="o">()</span> <span class="o">{</span>
<span class="indent">  </span><span class="indent">  </span><span class="kd"><span class="keyword" style="font-weight: bold;">super</span></span><span class="o">.</span><span class="na">onDetach</span><span class="o">();</span>
<span class="indent">  </span><span class="indent">  </span><span class="k"><span class="keyword" style="font-weight: bold;">try</span></span> <span class="o">{</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="n">Field</span> <span class="n">childFragmentManager</span> <span class="o">=</span> <span class="n">Fragment</span><span class="o">.</span><span class="na"><span class="class" style="color: rgb(68, 85, 136); font-weight: bold;"><span class="keyword" style="color: rgb(51, 51, 51);">class</span></span></span><span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="o">.</span><span class="na"><span class="title">getDeclaredField</span></span><span class="o">(</span><span class="s">"<span class="title">mChildFragmentManager</span>"</span><span class="o">);</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="n"><span class="title">childFragmentManager</span></span><span class="o">.</span><span class="na"><span class="title">setAccessible</span></span><span class="o">(</span><span class="kc"><span class="title">true</span></span><span class="o">);</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="n"><span class="title">childFragmentManager</span></span><span class="o">.</span><span class="na"><span class="title">set</span></span><span class="o">(</span><span class="k"><span class="title">this</span></span><span class="o">,</span> <span class="kc"><span class="title">null</span></span><span class="o">);</span>

<span class="indent">  </span><span class="indent">  </span><span class="o">}</span> <span class="k"><span class="title">catch</span></span> <span class="o">(</span><span class="n"><span class="title">NoSuchFieldException</span></span> <span class="n"><span class="title">e</span></span><span class="o">)</span> <span class="o">{</span></span><span class="o"></span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="k"><span class="keyword" style="font-weight: bold;">throw</span></span> <span class="k"><span class="keyword" style="font-weight: bold;">new</span></span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="n">e</span><span class="o">);</span>
<span class="indent">  </span><span class="indent">  </span><span class="o">}</span> <span class="k"><span class="keyword" style="font-weight: bold;">catch</span></span> <span class="o">(</span><span class="n">IllegalAccessException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="k"><span class="keyword" style="font-weight: bold;">throw</span></span> <span class="k"><span class="keyword" style="font-weight: bold;">new</span></span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="n">e</span><span class="o">);</span>
<span class="indent">  </span><span class="indent">  </span><span class="o">}</span>
<span class="indent">  </span><span class="o">}</span>

Case 2:当我们从一个Activity启动了一个Fragment,然后在这个Fragment中又去实例化了一些子Fragment,在子Fragment中去有返回的启动了另外一个Activity,即通过startActivityForResult方式去启动,这时候造成的现象会是,子Fragment接收不到OnActivityResult,如果在子Fragment中是以getActivity.startActivityForResult方式启动,那么只有Activity会接收到OnActivityResult,如果是以getParentFragment.startActivityForResult方式启动,那么只有父Fragment能接收(此时Activity也能接收),但无论如何子Fragment接收不到OnActivityResult。

这是一个非常奇怪的现象,按理说,应该是让子Fragment接收到OnActivityResult才对,究竟是什么造成的呢?这是由于某位写代码的员工抱怨没发奖金,稍稍偷懒了,少写了一部分代码,没有考虑到Fragment再去嵌套Fragment的情况。

我们来看看FragmentActivity中的代码:

<span class="kd"><span class="keyword" style="font-weight: bold;">protected</span></span> <span class="kt"><span class="keyword" style="font-weight: bold;">void</span></span> <span class="nf">onActivityResult</span><span class="o">(</span><span class="kt"><span class="keyword" style="font-weight: bold;">int</span></span> <span class="n">requestCode</span><span class="o">,</span> <span class="kt"><span class="keyword" style="font-weight: bold;">int</span></span> <span class="n">resultCode</span><span class="o">,</span> <span class="n">Intent</span> <span class="n">data</span><span class="o">)</span>
  <span class="o">{</span>
    <span class="k"><span class="keyword" style="font-weight: bold;">this</span></span><span class="o">.</span><span class="na">mFragments</span><span class="o">.</span><span class="na">noteStateNotSaved</span><span class="o">();</span>
    <span class="kt"><span class="keyword" style="font-weight: bold;">int</span></span> <span class="n">index</span> <span class="o">=</span> <span class="n">requestCode</span> <span class="o">>></span> <span class="mi"><span class="number" style="color: rgb(0, 153, 153);">16</span></span><span class="o">;</span>
    <span class="k"><span class="keyword" style="font-weight: bold;">if</span></span> <span class="o">(</span><span class="n">index</span> <span class="o">!=</span> <span class="mi"><span class="number" style="color: rgb(0, 153, 153);">0</span></span><span class="o">)</span> <span class="o">{</span>
      <span class="n">index</span><span class="o">--;</span>
      <span class="k"><span class="keyword" style="font-weight: bold;">if</span></span> <span class="o">((</span><span class="k"><span class="keyword" style="font-weight: bold;">this</span></span><span class="o">.</span><span class="na">mFragments</span><span class="o">.</span><span class="na">mActive</span> <span class="o">==</span> <span class="kc"><span class="keyword" style="font-weight: bold;">null</span></span><span class="o">)</span> <span class="o">||</span> <span class="o">(</span><span class="n">index</span> <span class="o"><</span> <span class="mi"><span class="number" style="color: rgb(0, 153, 153);">0</span></span><span class="o">)</span> <span class="o">||</span> <span class="o">(</span><span class="n">index</span> <span class="o">>=</span> <span class="k"><span class="keyword" style="font-weight: bold;">this</span></span><span class="o">.</span><span class="na">mFragments</span><span class="o">.</span><span class="na">mActive</span><span class="o">.</span><span class="na">size</span><span class="o">()))</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">w</span><span class="o">(</span><span class="s"><span class="string" style="color: rgb(221, 17, 68);">"FragmentActivity"</span></span><span class="o">,</span> <span class="s"><span class="string" style="color: rgb(221, 17, 68);">"Activity result fragment index out of range: 0x"</span></span> <span class="o">+</span> <span class="n">Integer</span><span class="o">.</span><span class="na">toHexString</span><span class="o">(</span><span class="n">requestCode</span><span class="o">));</span>

        <span class="k"><span class="keyword" style="font-weight: bold;">return</span></span><span class="o">;</span>
      <span class="o">}</span>
      <span class="n">Fragment</span> <span class="n">frag</span> <span class="o">=</span> <span class="o">(</span><span class="n">Fragment</span><span class="o">)</span><span class="k"><span class="keyword" style="font-weight: bold;">this</span></span><span class="o">.</span><span class="na">mFragments</span><span class="o">.</span><span class="na">mActive</span><span class="o">.</span><span class="na"><span class="keyword" style="font-weight: bold;">get</span></span><span class="o">(</span><span class="n">index</span><span class="o">);</span>
      <span class="k"><span class="keyword" style="font-weight: bold;">if</span></span> <span class="o">(</span><span class="n">frag</span> <span class="o">==</span> <span class="kc"><span class="keyword" style="font-weight: bold;">null</span></span><span class="o">)</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">w</span><span class="o">(</span><span class="s"><span class="string" style="color: rgb(221, 17, 68);">"FragmentActivity"</span></span><span class="o">,</span> <span class="s"><span class="string" style="color: rgb(221, 17, 68);">"Activity result no fragment exists for index: 0x"</span></span> <span class="o">+</span> <span class="n">Integer</span><span class="o">.</span><span class="na">toHexString</span><span class="o">(</span><span class="n">requestCode</span><span class="o">));</span>
      <span class="o">}</span>
      <span class="k"><span class="keyword" style="font-weight: bold;">else</span></span> <span class="o">{</span>
        <span class="n">frag</span><span class="o">.</span><span class="na">onActivityResult</span><span class="o">(</span><span class="n">requestCode</span> <span class="o">&</span> <span class="mi"><span class="number" style="color: rgb(0, 153, 153);">0</span></span><span class="number" style="color: rgb(0, 153, 153);">xFFFF</span><span class="n"></span><span class="o">,</span> <span class="n">resultCode</span><span class="o">,</span> <span class="n">data</span><span class="o">);</span>
      <span class="o">}</span>
      <span class="k"><span class="keyword" style="font-weight: bold;">return</span></span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">super</span><span class="o">.</span><span class="na">onActivityResult</span><span class="o">(</span><span class="n">requestCode</span><span class="o">,</span> <span class="n">resultCode</span><span class="o">,</span> <span class="n">data</span><span class="o">);</span>
  <span class="o">}</span>

很显然,设计者把Fragment的下标+1左移16位来标记这个request是不是Fragment的,拿到result再解码出下标,直接取对应的Fragment,这样并没有去考虑对Fragment嵌套Fragment做一个Map映射,所以出现了这种BUG。

但是如果我们需要在OnActivityResult的时候处理一些事情的话,我们可以通过在子Fragment中以getParentFragment.startActivityForResult的方式来启动,然后在父Fragment中去接收数据,我们需要在子Fragment中提供一个方法,如:getResultData(Object obj),通过父Fragment中的子Fragment实例去调用这个方法,把相应的数据传过去,然后去更新子Fragment。

以上是在使用Fragment去嵌套Fragment的时候可能会遇到的BUG,了解了BUG存在的原因之后,就可以完美的解决问题。

有时在fragment中获取getActivity()为null,可以将上下文设置成static类型。

23.在使用AndroidHttpClient时有如下错误:

01-16 22:18:39.222: E/AndroidHttpClient(26745): java.lang.IllegalStateException: AndroidHttpClient created and never closed

 这个主要是因为没有调用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语句块中调用其close方法即可。

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

     

最后更新:2012-11-12

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

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;
}

中,使用,注意带参数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
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://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
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://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
2
3
4
return  new  AlertDialog.Builder( this )
                 .setTitle( "提醒!" )
                 .setMessage(args.getString( "MESSAGE" ))
                 .setCancelable( false )

  但是,实际使用我发现,如果我是在Activity中托管使用这个的话,在整个Acitivty生命周期中只创建一次,所以,args.getString()就只能得到第一次得到内容.

解决办法是使用onPrepareDialog(),现在知道为什么要这样子设计了.

?
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的适配器中出现: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/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过去 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ToastUtils.showLong( this "正在加载楼层数据..." );
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()时经常被第地个参数给搞定了.我是在适配器上使用的:

?
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

应该改成下面这样 :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
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. 现在的一个需要是要在一个ListView的项中滑动时,就表示删除其中一个项.

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

ListView中的项对应布局文件如下 :(名为: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.android.com/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.但是我帮android的示例项目也是这样的.android资源中文章:

docs/resources/articles/gestures.

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

于是我就在自己实现的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中实现的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 完成删除
}

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

 

3.

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

有时候想运行测试一下一个简单的Java方法,但是呢今天发现了这样一个问题:

然后我就想通了.

报告错误如下 :

?
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.sun.com/webapps/bugreport/crash.jsp
#

 

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
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])

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

我呢?哎。


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

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

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

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

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

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

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

转自 http://my.oschina.net/banxi/blog/49562

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值