我只是使用TouchEvent方法创建一个用于在屏幕上书写文本的应用程序。它工作正常。看到这是代码
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(x, y);
break;
default:
return false;
}
invalidate();
return true;
之后,我编写了清除布局内容的代码:
LinearLayout li = (LinearLayout)findViewById(R.id.linear);
li.clearAnimation();
我的Logcat
11-18 16:10:18.681: I/System.out(22859): This is a log message
11-18 16:10:18.681: E/Mein Tag(22859): Hello
11-18 16:10:20.581: W/KeyCharacterMap(22859): No keyboard for id 0
11-18 16:10:20.581: W/KeyCharacterMap(22859): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-18 16:10:21.721: D/AndroidRuntime(22859): Shutting down VM
11-18 16:10:21.721: W/dalvikvm(22859): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-18 16:10:21.731: E/AndroidRuntime(22859): FATAL EXCEPTION: main
11-18 16:10:21.731: E/AndroidRuntime(22859): java.lang.NullPointerException
11-18 16:10:21.731: E/AndroidRuntime(22859): at de.vogella.android.touch.WriteOnScreenActivity.clear(WriteOnScreenActivity.java:47)
11-18 16:10:21.731: E/AndroidRuntime(22859): at de.vogella.android.touch.WriteOnScreenActivity.onOptionsItemSelected(WriteOnScreenActivity.java:38)
11-18 16:10:21.731: E/AndroidRuntime(22859): at android.app.Activity.onMenuItemSelected(Activity.java:2195)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
11-18 16:10:21.731: E/AndroidRuntime(22859): at android.view.View$PerformClick.run(View.java:8816)
11-18 16:10:21.731: E/AndroidRuntime(22859): at android.os.Handler.handleCallback(Handler.java:587)
11-18 16:10:21.731: E/AndroidRuntime(22859): at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 16:10:21.731: E/AndroidRuntime(22859): at android.os.Looper.loop(Looper.java:123)
11-18 16:10:21.731: E/AndroidRuntime(22859): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-18 16:10:21.731: E/AndroidRuntime(22859): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 16:10:21.731: E/AndroidRuntime(22859): at java.lang.reflect.Method.invoke(Method.java:521)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-18 16:10:21.731: E/AndroidRuntime(22859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-18 16:10:21.731: E/AndroidRuntime(22859): at dalvik.system.NativeStart.main(Native Method)
11-18 16:22:15.801: I/System.out(23179): This is a log message
11-18 16:22:15.811: E/Mein Tag(23179): Hello
当我按下清除按钮时,我为此应用程序获得了Nullpointer Exception。怎么了?
任何人指导我。
答案 0 :(得分:6)
LinearLayout lyt = (LinearLayout) findViewById(R.id.linearlayout);
lyt.removeAllViews();
答案 1 :(得分:5)
/*this is for clear viewgroup here main is viewgroup */
if (null != main && main.getChildCount() > 0) {
try {
main.removeViews (0, main.getChildCount());
} catch (Exception e) {
e.printStackTrace();
}
}
答案 2 :(得分:1)
我得到了(AndroidTechMe)的答案。我使用onCreate(null)
来清除内容。谢谢你的队友。
答案 3 :(得分:1)
这应该有效
LinearLayout layout; // also works with other Layouts or ViewGroups
...
layout.removeAllViews();
// layout is cleared
答案 4 :(得分:0)
看看这个link。它已在apidemos示例项目中提供。 希望这可以帮助你..