1、java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.AssetManager android.content.res.Resources.getAssets()’ on a null object reference抛出这个错误是在安装apk的时候就黑屏了,刚开始百思不得其解,明明还没有调用getAssets()就一直抛出这个异常,各种加日志加注释没找到问题所在。最后既然提示这个android.content.res.Resources为空,就在Application的onCreate()加判断发现getResource确实为空
RuntimeException: Unable to start activity ComponentInfo{com.../com...activity.SignInActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.res.Resources.getAssets()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.res.Resources.getAssets()' on a null object reference
at android.app.LoadedApk.getAssets(LoadedApk.java:528)
at android.app.LoadedApk.makeApplication(LoadedApk.java:584)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2243)
... 10 more
//解决方法:在Application的onCreate()加判断,getResource()为空的时候杀死当前进程
if (null == getResources()){
System.out.println("---********----------killProcess----2----***********---");
Process.killProcess(Process.myPid());
}
2、The content of the adapter has changed but ListView did not receive a notification,listview中adapter绑定的数据发生变化的时候UI还没更新就点击item就会抛出这个异常崩溃
选择类别的时候碰到列表崩溃
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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131427470, class android.widget.ListView) with Adapter(class com..adapter.ExportVideoAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1562)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4498)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:773)
at android.view.Choreographer.doCallbacks(Choreographer.java:586)
at android.view.Choreographer.doFrame(Choreographer.java:555)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:759)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
at Android.MODEL(msm8909)
at Android.VERSION(5.1.1)
at Android.FINGERPRINT(qcom/msm8909/msm8909:5.1.1/LMY47V/icar09111534:userdebug/test-keys)
//解决方法:在list数据改变的时候在主UI线程调用adapter的notifyDataSetChanged()通知数据改变了要更新UI,在
//notifyDataSetChanged()之前调用listview.setVisibility(View.GONE)之后调用 //listview.setVisibility(View.VISIBLE);
//代码如下:
listview.setVisibility(View.GONE);
adapter.notifyDataSetChanged();
listview.setVisibility(View.VISIBLE);
3.在维护一个比较老的项目的时候,发现一个问题,在发出请求的时候去切换网络APP崩溃了,看抛出的异常内容被消耗了,其实是EntityUtils.toString调用了两次,response只能一次消耗完,不能多次读取。
解决方法:直接使用trycacth抛出异常,或者保存response值,每次请求赋值,通过保存的response取值。
java.lang.IllegalStateException: Content has been consumed
at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:89)
at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:105)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:117)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:151)
at net.babelstar.common.http.AbstractAsyncResponseListener.onResponseReceived(AbstractAsyncResponseListener.java:57)
at net.babelstar.common.http.AsyncHttpSender.onPostExecute(AsyncHttpSender.java:117)
at net.babelstar.common.http.AsyncHttpSender.onPostExecute(AsyncHttpSender.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
4.Android中使用AS有时候会出现xml布局显示不出来,并报如下信息:
The following classes could not be instantiated:
- android.support.v7.widget.RecyclerView (Open Class, Show Exception, Clear Cache)
- android.support.v7.widget.Toolbar (Open Class, Show Exception, Clear Cache)
- android.support.v7.internal.widget.ActionBarContextView (Open Class, Show Exception, Clear Cache)
- android.support.v7.internal.app.WindowDecorActionBar (Open Class, Show Exception, Clear Cache)
- android.support.v7.internal.widget.ActionBarOverlayLayout (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout. Exception Details java.lang.ClassNotFoundException: android.support.v4.view.ViewPropertyAnimatorListener Copy stack to clipboard
**解决方法:**在style.xml中的<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> Theme前加Base.就可以了