0

嘿伙计们,你能看看我的错误吗,我的 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)

**

感谢你们。

4

3 回答 3

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 
     {
     }
}
于 2013-03-07T06:52:46.827 回答
0

我想因为这条线

new phpconnect().execute();

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

您使用 < String , String, String>声明 AsyncTask但您没有在 .exeute(); 中传递任何参数;

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

class phpconnect extends AsyncTask<Void, Void, Void> {
于 2013-03-07T13:06:13.353 回答
0

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

希望这会帮助你。

于 2013-03-07T06:57:45.383 回答