Android 2.3.3 Eclipse Version: 3.7.0 LogCat
AutoScrollTextView继承TextView,用于文字特殊效果显示,代码如下:
public class AutoScrollTextView extends TextView implements OnClickListener { …… public void onDraw(Canvas canvas) { …… canvas.drawText(text, temp_view_plus_text_length - step, y, paint); …… } …… }
Activity中使用:
taobaoTextView = (AutoScrollTextView) findViewById(R.id.text);
Activity对应的layout :
<com.taobao.util.AutoScrollTextView android:id="@+id/text" android:text="text" android:layout_height="fill_parent" android:layout_width="40px" android:textColor="#999" android:layout_gravity="right" android:background="#333"> </com.taobao.util.AutoScrollTextView>
当Activity中取消使用AutoScrollTextView,而layout 未删除有关AutoScrollTextView的配置时,
LogCat 报错信息:
02-07 12:06:58.728: DEBUG/AndroidRuntime(1034): Shutting down VM 02-07 12:06:58.728: WARN/dalvikvm(1034): threadid=1: thread exiting with uncaught exception (group=0x40015560) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): FATAL EXCEPTION: main 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): java.lang.NullPointerException 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.graphics.Canvas.drawText(Native Method) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at com.taobao.util.AutoScrollTextView.onDraw(AutoScrollTextView.java:181) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.View.draw(View.java:6880) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.View.draw(View.java:6883) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewRoot.draw(ViewRoot.java:1522) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.os.Handler.dispatchMessage(Handler.java:99) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.os.Looper.loop(Looper.java:123) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at android.app.ActivityThread.main(ActivityThread.java:3683) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at java.lang.reflect.Method.invoke(Method.java:507) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 02-07 12:06:58.747: ERROR/AndroidRuntime(1034): at dalvik.system.NativeStart.main(Native Method) 02-07 12:06:58.797: WARN/ActivityManager(61): Force finishing activity com.taobao.tmall/.PlayAd 02-07 12:06:58.807: WARN/ActivityManager(61): Force finishing activity com.taobao.tmall/.WakeUp
发生错误原因分析:
canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
此处的paint为null,报错。
解决办法:在AutoScrollTextView.java中初始化paint,为其赋值。
paint = getPaint();