Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Clear content on the layout?

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.

like image 784
Praveenkumar Avatar asked Dec 06 '22 18:12

Praveenkumar


1 Answers

LinearLayout lyt = (LinearLayout) findViewById(R.id.linearlayout);
lyt.removeAllViews();
like image 98
Burak Durmuş Avatar answered Dec 28 '22 20:12

Burak Durmuş