-1

After reading over android asynctask sending callbacks to ui My goal is :

dialog box will show when you are waiting for the authentication from the server and as long as the process is done, a call back will be invoked and dialog box will be dismissed.

My structure is ( asyntask and log in activity are separated classes )

  1. Log in activity
  2. Class A extends Aysnctask
  3. A call back interface.

Partial of my codes :

public class SpalshScreenActivity extends Activity implements LogInCallBack{
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            logInButton = (Button) findViewById(R.id.btnLogin);
            logInButton.setBackgroundDrawable(getResources() .getDrawable(R.drawable.button));
            logInButton .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog = new ProgressDialog(SpalshScreenActivity.this);
                    dialog.setMessage("waiting......");
                    dialog.show();

                    Service client = new Service(new SpalshScreenActivity(), email.getText().toString(), password.getText().toString());
                    String logInURL = "my url goes here";

                    client.execute(logInURL);
                }
            });
        }
    }

    @Override
    public void successfullyLogIn() {
        Log.d("SPLASH","successfullyLogIn");
        dialog.dismiss(); // **<--------crash is at here**
    }

    @Override
    public void failedToLogIn() {

    }
}

Service.java :

public class Service extends AsyncTask<String, Integer, String> {
    private LogInCallBack callBack;
    String userName;
    String password;

    public Service(LogInCallBack callBack, String userName, String password) {  
        this.callBack = callBack;
        this.userName = userName;
        this.password = password;
    }
    // Handling ssl connection
protected String doInBackground(String... arg) {
    // validation and accept all type of self signed certificates
    SimpleSSLSocketFactory simpleSSLFactory;
    String  logInURL    =   arg[0];
    try {
        simpleSSLFactory = new SimpleSSLSocketFactory(null);

        simpleSSLFactory .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);

        // Enable HTTP parameters
        params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        // Register the HTTP and HTTPS Protocols. For HTTPS, register our
        // custom SSL Factory object.
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80));
        registry.register(new Scheme("https", simpleSSLFactory, 443));

        // Create a new connection manager using the newly created registry
        // and then create a new HTTP client
        // using this connection manager
        ccm = new ThreadSafeClientConnManager(params, registry);
    } catch (KeyManagementException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (UnrecoverableKeyException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (KeyStoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // TODO Auto-generated method stub
    HttpClient client = new DefaultHttpClient(ccm, params);

    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    nameValuePair.add(new BasicNameValuePair("user_session[email]", "lenanguyen@hotmail.ca"));
    nameValuePair.add(new BasicNameValuePair("user_session[password]","2congato"));

    // Create http GET method
    HttpPost logIn = new HttpPost(logInURL);

    try {
        logIn.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // Fire a request
    try {
        HttpResponse response = client.execute(logIn);
        HttpEntity entity = response.getEntity();

        InputStream is = entity.getContent();
        this.result = convertStreamToString(is);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("ClientProtocolException is ", e.toString());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("IOException is ", e.toString());

    }
    return result;
}

    // called after doInBackground finishes
    protected void onPostExecute(String result) {   
        Log.e("result, yay!", this.result);
        this.callBack.successfullyLogIn();
    }
 }

and my call back interface is

public interface LogInCallBack {
    void successfullyLogIn ();
    void failedToLogIn();
}

However after the dialog shows up for a few seconds, my app is crashing and FATAL Exception in MAIN is thrown in the cat log. Any ideas about this guys.

PS :

log cat i here

logcat from link

Community
  • 1
  • 1
tranvutuan
  • 6,089
  • 8
  • 47
  • 83

1 Answers1

2

Change

Service client = new Service(new SpalshScreenActivity(), email.getText().toString(), password.getText().toString());

into

Service client = new Service(SpalshScreenActivity.this, email.getText().toString(), password.getText().toString());

and

public FlipGiveClient(LogInCallBack callBack, String userName, String password) {  
    this.callBack = callBack;
    this.userName = userName;
    this.password = password;
}

into

public FlipGiveClient(Activity activity, String userName, String password) {  
    this.callBack = (LogInCallBack) activity;
    this.userName = userName;
    this.password = password;
}
DroidBender
  • 7,762
  • 4
  • 28
  • 37
  • i dont get it when you change from LogInCallBack ----> Activity in constructor. LogInCallBack is just an interface and it is not Activity at all. How does it work....Can you explain pls – tranvutuan May 08 '13 at 14:43
  • 2
    the problem you have (that @Martijn identified correctly) is that when the user clicks the button you are creating a new SpalshScreenActivity, whose dialog therefore never has been initialized (i.e. has a null value). Which is why he tells you to use the one you already have as a parameter in your invocation. – DigCamara May 08 '13 at 16:20