-1

我有这个登录活动,其中包含执行解析方法(发布)的异步任务类,一切正常,我的数据已发布,但是当用户输入错误的电子邮件和密码时,我只需要向他敬酒,说他的电子邮件和密码是错误的所以我的代码如下所示:

public class ActivityLogin extends Activity 
{
// Progress Dialog
    private ProgressDialog pDialog;
    private static String login_tag = "login";
    PostParser jsonParser = new PostParser();
    // UI references.
    private EditText email;
    private EditText password;
    static String Email="";
    static String Password="";
    // url to create new product
    private static String login_URL = "jhdakjhdakj";
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_login);

        email=(EditText)findViewById(R.id.email);
        password=(EditText)findViewById(R.id.password1);

        Button login=(Button)findViewById(R.id.sign_in_button);
        login.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Email=email.getText().toString();
                Password=password.getText().toString();
                (new LoginInTask()).execute();

            }
        });


    }
    class LoginInTask extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ActivityLogin.this);
            pDialog.setMessage("Logging IN..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) 
    {
        String Email = email.getText().toString();
        String Password = password.getText().toString();
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("Email", Email));
        params.add(new BasicNameValuePair("Password", Password));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(login_URL,"POST", params);


        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag

            int success=0;
            try 
            {
                success = json.getInt(TAG_SUCCESS);
            } catch (JSONException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(int success) 
    {
        if (success==1) 
        {   
            // successfully created product
            Intent i = new Intent(getApplicationContext(), ActivitySignUp.class);
            startActivity(i);

            // closing this screen
            pDialog.dismiss();
            finish();
        }
        else 
        {                   

              Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();

        }
        pDialog.dismiss();
        finish();
    }

}
}

您的帮助将不胜感激。谢谢

这是我的日志猫

 11-05 16:16:46.536: E/AndroidRuntime(5807): FATAL EXCEPTION: AsyncTask #1
 11-05 16:16:46.536: E/AndroidRuntime(5807): java.lang.RuntimeException: An error occured while   executing doInBackground()
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at   java.util.concurrent.FutureTask.setException(FutureTask.java:219)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at  java.util.concurrent.FutureTask.run(FutureTask.java:239)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at  android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at java.lang.Thread.run(Thread.java:849)
 11-05 16:16:46.536: E/AndroidRuntime(5807): Caused by: java.lang.NullPointerException
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at  com.asap.ActivityLogin$LoginInTask.doInBackground(ActivityLogin.java:108)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at com.asap.ActivityLogin$LoginInTask.doInBackground(ActivityLogin.java:1)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 11-05 16:16:46.536: E/AndroidRuntime(5807):    ... 4 more
4

1 回答 1

1

请用这个更改您的代码我已经为您修复了许多标记为<--jack

public class ActivityLogin extends Activity 
{
// Progress Dialog
    private ProgressDialog pDialog;
    private static String login_tag = "login";
    PostParser jsonParser;//<-- placed in doinbackground <-- jack
    // UI references.
    private EditText email;
    private EditText password;
    static String Email="";
    static String Password="";
    // url to create new product
    private static String login_URL = "jhdakjhdakj";
    // JSON Node names
    private static final String TAG_SUCCESS = "success";


    //Query login <<- jack
    LoginInTask query;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_login);

        email=(EditText)findViewById(R.id.email);
        password=(EditText)findViewById(R.id.password1);

        Button login=(Button)findViewById(R.id.sign_in_button);
        login.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Email=email.getText().toString();
                Password=password.getText().toString();

                //Cancel if already runing or dont excute its up to you <-- jack
                if( query != null )
                {
                    query.cancel(true);
                }
                query = ( LoginInTask ) new LoginInTask().execute();

            }
        });


    }
    class LoginInTask extends AsyncTask<String, String, String> {

        //Register success here <-- jack
          int success=0;

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ActivityLogin.this);
            pDialog.setMessage("Logging IN..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        /**
         * if canceled <-- jack
         * */
        @Override
        protected void onCancelled() {
            if ( pDialog != null )
            {
                 pDialog.dismiss();
            }
        }
        /**
         * Creating product
         * */
        protected String doInBackground(String... args) 
      {
        String Email = email.getText().toString();
        String Password = password.getText().toString();
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("Email", Email));
        params.add(new BasicNameValuePair("Password", Password));

        //Try from here coz you are working with JSON <<-- jack
        try 
        {
        // getting JSON Object
        // Note that create product url accepts POST method

        //Start new Session <-- jack
        jsonParse   = new PostParser();
        JSONObject json = jsonParser.makeHttpRequest(login_URL,"POST", params);


        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag

                success = json.getInt(TAG_SUCCESS);
            } catch (JSONException e) 
            {
                Log.d("Create Response", "Faild");//<-- tag faild to parse JSON
                e.printStackTrace();
            }


        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(int success) 
    {
        //Check if dialog still runing dismiss <<-- jack 
        if ( pDialog != null)
        {
            pDialog.dismiss();  
        }

        if (success==1) 
        {   
            // successfully created product
            Intent i = new Intent(getApplicationContext(), ActivitySignUp.class);
            startActivity(i);
        }
        else 
        {                   
            Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
        }
        //finish current activity call it on time only <<-- jack 
        finish();
    }

}
}
于 2013-11-05T00:15:25.323 回答