在AsyncTask中获得空指针异常?

时间:2013-03-07 06:50:29

标签: java android nullpointerexception

嘿伙计们,你能看看我的错误,我的logcat说是因为我得到空指针异常。我不明白为什么。我刚刚添加了一个字符串变量“电子邮件”,并使我的eadd的值代表电子邮件地址等于它,然后我现在有这些错误。你能指出我的问题吗?

这些是我的代码:

**

public class LoginSub extends Activity {
     public static String Email;
    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputEmail;
    EditText inputPassword;
    TextView TextView1;

    // url to create new product
    //private static String url_create_product = "http://student-thesis.netii.net/log_in.php";
    private static String url_create_product = "http://10.0.2.2/TheCalling/log_in.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Edit Text
        inputEmail = (EditText) findViewById(R.id.inputEmail);
        inputPassword = (EditText) findViewById(R.id.inputPassword);


        // Create button
        Button btnLogin = (Button) findViewById(R.id.btnLogin);

        // button click event
        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new phpconnect().execute();
            }
        });

        Button btnReg = (Button) findViewById(R.id.btnReg);
        // button click event
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), Register.class);
                startActivity(i);
            }
        });
    }

    /**
     * Background Async Task to Create new product
     * */
    class phpconnect extends AsyncTask<String, String, String> {

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

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String eadd = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();


            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("eadd", eadd));
            params.add(new BasicNameValuePair("password", password));

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

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

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
                Email.equals(eadd);
                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), Mapping.class);
                    startActivity(i);
                    // closing this screen
                    finish();
                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }
}

**

这是我的logcat错误:

**

03-06 22:21:16.428: E/AndroidRuntime(1417): FATAL EXCEPTION: AsyncTask #1
03-06 22:21:16.428: E/AndroidRuntime(1417): java.lang.RuntimeException: An error occured while executing doInBackground()
03-06 22:21:16.428: E/AndroidRuntime(1417):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.lang.Thread.run(Thread.java:856)
03-06 22:21:16.428: E/AndroidRuntime(1417): Caused by: java.lang.NullPointerException
03-06 22:21:16.428: E/AndroidRuntime(1417):     at com.example.projectthesis.LoginSub$phpconnect.doInBackground(LoginSub.java:129)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at com.example.projectthesis.LoginSub$phpconnect.doInBackground(LoginSub.java:1)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-06 22:21:16.428: E/AndroidRuntime(1417):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

**

谢谢你们。

3 个答案:

答案 0 :(得分:1)

doInBackGround()

中删除此块
if (success == 1) {
    // successfully created product
    Intent i = new Intent(LoginSub.this, Mapping.class);
    startActivity(i);
    // closing this screen
    finish();
} else {
  }

并将其添加到onPostExecute()部分

protected void onPostExecute(String file_url) 
{
    // dismiss the dialog once done
    pDialog.dismiss();
    if (success == 1) 
    {
       // successfully created product
       Intent i = new Intent(getApplicationContext(), Mapping.class);
       startActivity(i);
       // closing this screen
       finish();
     } 
     else 
     {
     }
}

答案 1 :(得分:0)

有时 getApplicationContext()不会提供确切的上下文值。通过了 上下文背景; //宣布 context = this; //在setcontextview之后分配下一行。 然后传递上下文而不是getApplicationContext()。并检查它,如果你想做任何UI更新,你不应该在doinBackground()。你必须在post执行。

希望这会对你有所帮助。

答案 2 :(得分:0)

我认为因为这行

new phpconnect().execute();

class phpconnect extends AsyncTask<String, String, String> {

使用&lt; String ,String,String&gt;声明AsyncTask;但你没有在.exeute();

中传递任何参数

此外,从你的代码中你不需要AsyncTask的任何参数,所以你应该像这样声明你的Asynctask

class phpconnect extends AsyncTask<Void, Void, Void> {