I am a beginner, so please explain your answer clearly. Thanks

***EDIT 2***
I now have 

```
public class MainActivity extends Activity {

        //other declarations
        public ReadTask readTask;
    protected void onCreate(Bundle savedInstanceState){
     readTask = new ReadTask();

    private OnClickListener test = new OnClickListener()
        {
            @Override
            public void onClick (View V)
            {
                readTask.execute();
                //new ReadTask().execute();

            }

    };
    private OnClickListener cancel = new OnClickListener()
    {
        @Override
        public void onClick (View V)
        {   
            readTask.cancel(true);
            results1.setText("");
        }

```

Check EDIT 1 for the error this code above (Edit 2) throws;
Thanks

> 
> I have a cancel button in the App. On clicking this, this is what
>  happens;
> 
> 
> 

```
    public void onClick (View V)
        {
                    InputMethodManager inputManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE); 
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                       InputMethodManager.HIDE_NOT_ALWAYS);

            new ReadTask().onCancelled();
            new ReadTask().cancel(true);
            results1.setText("Cancelled");
        }

```

ReadTask extends AsyncTask like this;

```
public class ReadTask extends AsyncTask<Void, String, Void>
    {
    BufferedReader reader = null;
    public volatile boolean isCancelled=false;
    //other declarations
    protected void onCancelled() {
        super.onCancelled();
        isCancelled = true;
        new ReadTask().cancel(true);
    }

 @Override
    protected Void doInBackground(Void... params) 
    {
            // isCancelled=false;

        try {
            for(int k=x1[3];k<=x2[3];k++)
            {
                //bunch of code
            Log.w("5.newString", newString);
            String isC1 = String.valueOf(isCancelled);
            Log.w("5.Is cancelled?", isC1); // at this point, VALUE IS ALWAYS FALSE !

            if (isCancelled() || isCancelled == true) {Log.d("Entered","WHY");break;}

                //bunch of code

           } //bunch of catches

```

There are 2 methods tried here, one a boolean variable isCancelled and the other ReadTask().cancel(true); - Both don't work. Why?

I am a newcomer, so please explain your answer clearly. Thanks

\*\*\*\*\*\*\*\*\*\*\*\*\*\* EDIT 1 - Logcat Output\*\*\*\*\*\*\*

```
11-27 17:18:56.626: W/dalvikvm(28775): threadid=1: thread exiting with uncaught exception (group=0x41bcee48)
11-27 17:18:56.636: E/AndroidRuntime(28775): FATAL EXCEPTION: main
11-27 17:18:56.636: E/AndroidRuntime(28775): Process: com.example.pingtest, PID: 28775
11-27 17:18:56.636: E/AndroidRuntime(28775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pingtest/com.example.pingtest.MainActivity}: java.lang.NullPointerException
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.os.Looper.loop(Looper.java:136)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.ActivityThread.main(ActivityThread.java:5103)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at java.lang.reflect.Method.invokeNative(Native Method)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at java.lang.reflect.Method.invoke(Method.java:515)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at dalvik.system.NativeStart.main(Native Method)
11-27 17:18:56.636: E/AndroidRuntime(28775): Caused by: java.lang.NullPointerException
11-27 17:18:56.636: E/AndroidRuntime(28775):    at com.example.pingtest.MainActivity$ReadTask.<init>(MainActivity.java:120)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at com.example.pingtest.MainActivity.onCreate(MainActivity.java:39)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.Activity.performCreate(Activity.java:5275)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-27 17:18:56.636: E/AndroidRuntime(28775):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
11-27 17:18:56.636: E/AndroidRuntime(28775):    ... 11 more
11-27 17:19:05.665: I/Process(28775): Sending signal. PID: 28775 SIG: 9

```

---