android - dialog.show(); throws null pointer exception in Asynctask -


i tried access asynctask other class. problem pass context file of mainactivity asynctask class create progress dialog. dialog.show(); in sendtowebservice throws nullpointerexception

mainactivity

public class mainactivity extends actionbaractivity implements ontaskcompletion{      ontaskcompletion mcompletion;         textview tv;         @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);              mcompletion = this;              setcontentview(r.layout.activity_main);           try {               new sendtowebservice(getapplicationcontext(), mcompletion).execute();              }catch(exception e){              string error=e.tostring();               string error2=error;            }}          public void ontaskcompleted(string value) {             toast.maketext(mainactivity.this, value,                     toast.length_long).show();             tv=(textview)findviewbyid(r.id.textview1);             tv.settext(value);          }         } 

sendtowebservices.java

public class sendtowebservice extends asynctask<void, string, string> {      string response;     private context context;     progressdialog dialog;     private ontaskcompletion mcompletion;      public sendtowebservice(context cxt, ontaskcompletion mcompletion) {         context = cxt;         dialog = new progressdialog(context);         this.mcompletion=mcompletion;      }      @override     protected void onpreexecute() {          dialog.settitle("please wait");         dialog.show();     }      @override     protected string doinbackground(void... unused) {         systemclock.sleep(2000);          response="success";         return response;    }      @override     protected void onpostexecute(string response)      {         dialog.dismiss();        try {         mcompletion.ontaskcompleted(response);        }catch (exception e){             string error=e.tostring();            string error2=error;        } //        return response;     } }        ontaskcompletion.java 

interface file

package com.example.sampleprogressdialog;  public abstract  interface ontaskcompletion {      void ontaskcompleted(string response); //    void ontaskcompleted2(string response); } 

xml file

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/container"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.example.sampleprogressdialog.mainactivity"     tools:ignore="mergerootframe" >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="textview" />  </framelayout> 

logcat:

[2014-04-09 11:41:40 - sampleprogressdialog] uploading sampleprogressdialog.apk onto device 'emulator-5554' [2014-04-09 11:41:41 - sampleprogressdialog] installing sampleprogressdialog.apk... [2014-04-09 11:41:52 - sampleprogressdialog] success! [2014-04-09 11:41:52 - sampleprogressdialog] starting activity com.example.sampleprogressdialog.mainactivity on device emulator-5554 [2014-04-09 11:42:14 - sampleprogressdialog] success! [2014-04-09 11:42:15 - sampleprogressdialog] starting activity com.example.sampleprogressdialog.mainactivity on device emulator-5554 [2014-04-09 11:42:18 - sampleprogressdialog] activitymanager: starting: intent { act=android.intent.action.main cat=[android.intent.category.launcher] cmp=com.example.sampleprogressdialog/.mainactivity } 

try this

replace line:

from

new sendtowebservice(getapplicationcontext(), mcompletion).execute(); 

to

new sendtowebservice(this, mcompletion).execute(); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -