The quiter you become,the more you are able to hear!

Android开发中错误解决(持续更新)

Author: geneblue

Blog: https://geneblue.github.io/

平时写Android程序时遇到的一些错误, 在此记录方便以后查找。

类中方法找不到问题

logcat中报错提示如下: java.lang.NoClassDefFoundError: com.google.zxing.EncodeHintType

这个问题是在二维码编码中遇到的但是在代码使用的jar包中明明已经定义了该类,就是提示找不到该类,最后查找别人的解决办法,将jar文件存放在libs文件夹下就顺利解决了,之前我将其存放在了自定义的lib文件夹下。可问题是为什么别的jar文件放在lib下依然可以顺利使用jar中的类呢????????

Activity无法启动的错误

遇见的错误信息如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.security.ass.privacy/com.security.ass.ui.SplashActivity}: java.lang.NullPointerException
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.os.Looper.loop(Looper.java:153)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.ActivityThread.main(ActivityThread.java:5297)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at java.lang.reflect.Method.invokeNative(Native Method)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at java.lang.reflect.Method.invoke(Method.java:511)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at dalvik.system.NativeStart.main(Native Method)
06-30 16:41:03.958: E/AndroidRuntime(10588): Caused by: java.lang.NullPointerException
06-30 16:41:03.958: E/AndroidRuntime(10588):    at com.security.ass.ui.SplashActivity.onCreate(SplashActivity.java:31)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.Activity.performCreate(Activity.java:5122)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
06-30 16:41:03.958: E/AndroidRuntime(10588):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-30 16:41:03.958: E/AndroidRuntime(10588):    ... 12 more

始终提示一个空指针错误,但真实原因是在其他activity中定义的主题style与程序定义的注意style冲突。也就是说,主题最好不要混用,虽然程序编译不会报错,但运行时可能会出现很奇怪的错误

LinearLayout布局文件出错

提示的错误信息如下:

No orientation specified, and the default is horizontal. This is a common source of bugs when children are added dynamically.

从提示的信息中可以看出,linearLayout的方向未确定,默认是水平方向。当该LinearLayout中动态添加控件时候,这可能会触发一个bug。

解决:添加linearLayout的方向信息即可: android:orientation = "vertical"android:orientation = "horizontal"

Android canvas以res下的图片做背景出错

错误如下:

07-13 11:03:29.428: E/AndroidRuntime(29848): FATAL EXCEPTION: Thread-1672
07-13 11:03:29.428: E/AndroidRuntime(29848): java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
07-13 11:03:29.428: E/AndroidRuntime(29848):    at android.graphics.Canvas.<init>(Canvas.java:127)
07-13 11:03:29.428: E/AndroidRuntime(29848):    at com.security.ass.accesscontrol.PictureLockerGameView.draws(PictureLockerGameView.java:65)
07-13 11:03:29.428: E/AndroidRuntime(29848):    at com.security.ass.accesscontrol.PictureLockerGameView.run(PictureLockerGameView.java:87)
07-13 11:03:29.428: E/AndroidRuntime(29848):    at java.lang.Thread.run(Thread.java:838)

解决: 出现Immutable bitmap passed to Canvas constructor错误的原因是如果不用copy的方法,直接引用会对资源文件进行修改,而android是不允许在代码里修改res文件里的图片 使用: BitmapFactory.decodeResource(getResources(), R.drawable.xiao).copy(Bitmap.Config.ARGB_8888, true); 替换: BitmapFactory.decodeResource(getResources(), R.drawable.xiao);

自定义title出错

将自定义的titile加入项目中后有时会出错,具体的错误信息如下:

07-02 16:25:38.518: E/AndroidRuntime(26837): FATAL EXCEPTION: main
07-02 16:25:38.518: E/AndroidRuntime(26837): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.security.ass.privacy/com.security.ass.otherfunction.OtherFunctionMainActivity}: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.ActivityThread.access$600(ActivityThread.java:168)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.os.Handler.dispatchMessage(Handler.java:107)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.os.Looper.loop(Looper.java:194)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.ActivityThread.main(ActivityThread.java:5398)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at java.lang.reflect.Method.invokeNative(Native Method)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at java.lang.reflect.Method.invoke(Method.java:525)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at dalvik.system.NativeStart.main(Native Method)
07-02 16:25:38.518: E/AndroidRuntime(26837): Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:248)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2705)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2953)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.Activity.setContentView(Activity.java:1892)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at com.security.ass.otherfunction.OtherFunctionMainActivity.onCreate(OtherFunctionMainActivity.java:22)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.Activity.performCreate(Activity.java:5168)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
07-02 16:25:38.518: E/AndroidRuntime(26837):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
07-02 16:25:38.518: E/AndroidRuntime(26837):    ... 12 more

这里错误多数也是自定义的title的Theme和对应acitivy的Theme不相符,可以在manifest中更改activity的style,如何删除该style