Pergunta

I tried to access Asynctask from other class. my problem is pass the context file of the mainActivity to the Asynctask class to 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 }
Foi útil?

Solução

Try this

Replace this line:

From

new SendToWebService(getApplicationContext(), mCompletion).execute();

To

new SendToWebService(this, mCompletion).execute();

Outras dicas

Simply change from

new SendToWebService(getApplicationContext(), mCompletion).execute();

to

new SendToWebService(MainActivity.this, mCompletion).execute();

because Dialog needs a activity context not application context.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top