开发者

Null pointer exception for async task

开发者 https://www.devze.com 2023-03-16 02:00 出处:网络
I am dealing with my web services and I got a strange problem and I couldn\'t able to solve this as I am new to android development. I am getting null pointer exception while I am showing the toast in

I am dealing with my web services and I got a strange problem and I couldn't able to solve this as I am new to android development. I am getting null pointer exception while I am showing the toast in onPostExecute() method of async task. some one please help me to fix this

if (result.booleanValue()) // here at this line I am getting the error in logcat//

Thanks in advance....!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ///////////male female buttons code/////////////
    Button regmalebtn = (Button) findViewById(R.id.regmalebtn); 
    regmalebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            gender="M";
            System.out.println("gender value after  male button click ="+gender);
            Log.v(TAG,"gender value on  male button click ="+gender );
        }
    });
    Button regfemalebtn = (Button)findViewById(R.id.regfemalebtn);
    regfemalebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            gender ="F";
            // System.out.println("gender value on  female button click ="+gender);
            Log.v(TAG,"gender value on  female button click ="+gender );
        }
    });

    ////////////male female button code will ends here/////////////

    Button signin = (Button) findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new RegisterTask().execute();
        };
    });
}


private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(Register.this);

    protected void onPreExecute() {
        this.dialog.setMessage("Registering...");
        this.dialog.show();
    }


    //  @SuppressWarnings("unused")
    protected Boolean doInBackground(final Void unused) {
        return this.register(); //don't interact with the ui!
    }

    private Boolean register() {
        ************some code for webservices************
    }

    return null;

}

protected void onPostExecute(final Boolean result) {
    if (this.dialog.isShowing()) {
        this.dialog.dismiss();
    }
    if (result.booleanValue()) {
        // show register success dialog
        // I m getting the error at above line

        Toast.makeText(Register.this, "Registerd", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(Register.this, "Try Again", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected Boolean doInBackground(Void... params) {
    // TODO Auto-generated method stub
    return register();
}


}   
}

These r the logcat errors

06-28 16:55:56.685: WARN/dalvikvm(708): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 16:55:56.695: ERROR/AndroidRuntime(708): FATAL EXCEPTION: main
06-28 16:55:56.695: ERROR/AndroidRuntime(708): java.lang.NullPointerException
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at com.soap.Register$RegisterTask.onPostExecute(Register.java:193)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at com.soap.Register$RegisterTask.onPostExecute(Register.java:1)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.AsyncTask.finish(AsyncTask.java:417)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at android.os.Looper.loop(Looper.java:123)
06-28 16:55:56.695: ERROR/AndroidRuntime(708):     at and开发者_如何转开发roid.app.ActivityThread.main(ActivityThread.java:4627)


You are obviously returning null from register method. Either return something that isn't null or make a null check before calling result.booleanValue().

You could do that like this:

if (result == null) {

}
else if (result.booleanValue()) {

}
else {

}

By the way, why do you need to return a Boolean and not boolean (primitive type, which can't be null).


Try using as argument of the Toast the name of your activity.class. I think in your case is Register.class instead of Register.this


Try passing the context into variable. Or try Register.this.getApplicationContext() or BaseContext. Check if the dialog is null, make the pass the values and create new dialog in the postExecute() if that does not overlap from your requirements :)


i've written this

protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

            Toast.makeText(
                    myclassname.this,
                    "Downloading of " + FILENAME
                            + " complete.", Toast.LENGTH_LONG).show();
            refreshList();


        }

and this is workning for me..

see this link for more detail
http://hi.baidu.com/hi_android/blog/item/fea149f90da7fdedfd037f56.html
http://www.screaming-penguin.com/node/7746


I got rid of the same Toast/Async/Nullpointe problem by launching Toasts like this:

Toast.makeText(getApplicationContext(), "Hello Toast!", Toast.LENGTH_SHORT).show();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号