How to Clear content on the layout?

微笑、不失礼 提交于 2020-01-04 05:56:15

问题


I just create a app for writing the text on Screen using TouchEvent method. It works fine. See this is code

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;

After, that i've written code for clear the content in layout : -

LinearLayout li = (LinearLayout)findViewById(R.id.linear);
    li.clearAnimation();

My 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

When i press the clear button, i got Nullpointer Exception for this app. What am doing wrong?

Anyone Guide me.


回答1:


LinearLayout lyt = (LinearLayout) findViewById(R.id.linearlayout);
lyt.removeAllViews();



回答2:


/*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();
    }
}



回答3:


I've got the answer from (AndroidTechMe). I used onCreate(null) to clear the contents. Thanks mate.




回答4:


This should work

LinearLayout layout; // also works with other Layouts or ViewGroups
...
layout.removeAllViews();
// layout is cleared


来源:https://stackoverflow.com/questions/8181191/how-to-clear-content-on-the-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!