1

我制作了一个使用 Web 服务(在 php 中)访问网络数据的应用程序。在 onPostExecute() 方法中,应用程序崩溃,因为结果为 null 并引发空指针异常。

我知道有很多类似的问题,但我仍然没有得到它。请帮帮我!谢谢!

代码

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

   if (result != null) {  // error**

                // Do JSON Parsing

       }else{

               //Show Warning

       }

}

日志猫:-

11-09 16:26:43.279: I/System.out(19358): Result =   
11-09 16:26:43.279: I/System.out(19358): null
11-09 16:26:43.289: W/System.err(19358): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray
11-09 16:26:43.289: W/System.err(19358):    at org.json.JSON.typeMismatch(JSON.java:111)
11-09 16:26:43.289: W/System.err(19358):    at org.json.JSONArray.<init>(JSONArray.java:91)
11-09 16:26:43.289: W/System.err(19358):    at org.json.JSONArray.<init>(JSONArray.java:103)
11-09 16:26:43.289: W/System.err(19358):    at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:452)
11-09 16:26:43.289: W/System.err(19358):    at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:1)
11-09 16:26:43.289: W/System.err(19358):    at android.os.AsyncTask.finish(AsyncTask.java:631)
11-09 16:26:43.289: W/System.err(19358):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-09 16:26:43.289: W/System.err(19358):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-09 16:26:43.289: W/System.err(19358):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:26:43.289: W/System.err(19358):    at android.os.Looper.loop(Looper.java:137)
11-09 16:26:43.289: W/System.err(19358):    at android.app.ActivityThread.main(ActivityThread.java:4935)
11-09 16:26:43.289: W/System.err(19358):    at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:26:43.289: W/System.err(19358):    at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:26:43.299: W/System.err(19358):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-09 16:26:43.299: W/System.err(19358):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-09 16:26:43.299: W/System.err(19358):    at dalvik.system.NativeStart.main(Native Method)
11-09 16:26:43.399: D/AndroidRuntime(19358): Shutting down VM
11-09 16:26:43.399: W/dalvikvm(19358): threadid=1: thread exiting with uncaught exception (group=0x4106a2a0)
11-09 16:26:43.409: E/AndroidRuntime(19358): FATAL EXCEPTION: main
11-09 16:26:43.409: E/AndroidRuntime(19358): java.lang.NullPointerException
11-09 16:26:43.409: E/AndroidRuntime(19358):    at com.Auto.Login$AppTask.onPostExecute(Login.java:489)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at com.Auto.Login$AppTask.onPostExecute(Login.java:1)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at android.os.AsyncTask.finish(AsyncTask.java:631)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at android.os.Looper.loop(Looper.java:137)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at android.app.ActivityThread.main(ActivityThread.java:4935)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-09 16:26:43.409: E/AndroidRuntime(19358):    at dalvik.system.NativeStart.main(Native Method)

编辑:- 整个代码

public class AppTask extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (result != null) {

            if (forgt == 1) {

                if (result.contains("")) {
                    t5.setVisibility(View.VISIBLE);
                    et_f.setText("");
                    // dialog.dismiss();

                } else {
                    Toast.makeText(Login.this, "Mail Sent",
                            Toast.LENGTH_LONG).show();
                    myDialog.dismiss();
                    // dialog.dismiss();
                    forgt = 0;
                }
            } else {

                // dialog.dismiss();

                // Toast.makeText(Login.this, "Mail sent..!", 1000).show();
                System.out.println("Result = " + result);
                if (result.equalsIgnoreCase("null")) {

                    t4.setVisibility(View.VISIBLE);
                    et2.setText("");
                } else {
                    try {

                        JSONArray jArray = new JSONArray(result);

                        for (int i = 0; i < jArray.length(); i++) {

                            JSONObject jObject = jArray.getJSONObject(i);
                            s_id = jObject.getString("seller_id");
                            usr = jObject.getString("email");
                            pwd = jObject.getString("password");
                            type = jObject.getString("seller_Type");
                            code = jObject.getString("verification_Code");

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

                    Editor edit = sp.edit();
                    edit.putString("logID", s_id);
                    edit.commit();

                    Editor edit2 = sp1.edit();
                    edit2.putString("logPass", pwd);
                    edit2.commit();

                    Editor edit3 = sp2.edit();
                    edit3.putString("logEmail", usr);
                    edit3.commit();

                    Editor edit4 = sp3.edit();
                    edit4.putString("logType", type);
                    edit4.commit();

                    String ty = sp1.getString("logType", "");

                    Toast.makeText(getApplicationContext(), "Type: " + ty,
                            Toast.LENGTH_LONG).show();

                    if (code.contains("Verified")) {
                        V = new view_cars();
                        V.SetId(s_id);

                        Intent myIntent = new Intent(Login.this,
                                UserActivity.class);
                        startActivity(myIntent);
                    }

                    else {
                        // dialog.dismiss();
                        // Toast.makeText(getApplicationContext(),
                        // "NOT VERIFIED",
                        // Toast.LENGTH_LONG).show();
                        myDialog = new Dialog(Login.this);
                        myDialog.setContentView(R.layout.ask_pin);
                        myDialog.setTitle("Confirmation");
                        myDialog.setCancelable(true);

                        // for OK
                        Button ok = (Button) myDialog
                                .findViewById(R.id.button1);
                        et_pin = (EditText) myDialog
                                .findViewById(R.id.editText1);
                        ok.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                pin = et_pin.getText().toString();
                                Toast.makeText(getApplicationContext(),
                                        "CLICKED OK= " + pin,
                                        Toast.LENGTH_LONG).show();
                                if (pin.contains("null")) {
                                    Toast.makeText(getApplicationContext(),
                                            "Please enter the Pin",
                                            Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(
                                            getApplicationContext(),
                                            "email,pass,pin= " + str1 + ","
                                                    + str2 + "," + pin,
                                            Toast.LENGTH_LONG).show();

                                    new App_pin_Task()
                                            .execute(FILENAME_pin);
                                }
                            }
                        });
                        Button resend = (Button) myDialog
                                .findViewById(R.id.button2);
                        resend.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {

                                new App_resend_Task()
                                        .execute(FILENAME_resend);
                            }
                        });
                        Button cancel = (Button) myDialog
                                .findViewById(R.id.button3);
                        cancel.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                Toast.makeText(getApplicationContext(),
                                        "CLICKED CANCEL", Toast.LENGTH_LONG)
                                        .show();

                                et2.setText("");
                                myDialog.dismiss();

                                // Intent intent = new
                                // Intent(Dealer_details.this,
                                // Login.class);
                                // startActivity(intent);
                            }
                        });
                        myDialog.show();
                    }
                }
            }

        } else {

            Toast.makeText(getApplicationContext(),
                    "Network Congestion! Please try again later!",
                    Toast.LENGTH_LONG).show();
        }

        dialog.dismiss();

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {
        // String is = null;

        if (forgt == 1) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(FILENAME_f);
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                nameValuePairs.add(new BasicNameValuePair("email", email));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = EntityUtils.toString(entity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

        } else {
            str1 = et1.getText().toString();
            str2 = et2.getText().toString();

            if (str1.length() > 0 && str2.length() > 0) {
                A = str1;
                B = str2;
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://animsinc.com/login.php");
                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            2);
                    nameValuePairs.add(new BasicNameValuePair("username",
                            str1));
                    nameValuePairs.add(new BasicNameValuePair("password",
                            str2));
                    httppost.setEntity(new UrlEncodedFormEntity(
                            nameValuePairs));
                    httpclient.execute(httppost);

                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = EntityUtils.toString(entity);

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
        }
        return is;
    }

}
4

4 回答 4

1
11-09 16:26:43.289: W/System.err(19358):    at com.MyDreamAuto.Login$AppTask.onPostExecute(Login.java:452)

那条线是什么?您发布的代码有 225 行。

您可能正在使用View( setText()on it or such),但找不到它。

于 2014-03-17T21:40:26.127 回答
1

我认为问题可能是结果为空时的新 JSONArray(result)。

http://www.json.org/javadoc/org/json/JSONArray.html#JSONArray(java.lang.String)

您可能想要的是 !result.isEmpty() 而不是 equalsIgnoreCase("null")。

于 2014-09-22T10:08:47.523 回答
0

我怀疑您是否已将以下语句放在注释行中

//String is = null

那么变量是如何存储值的???

于 2013-11-09T10:57:32.693 回答
0

根据您发布的内容,您似乎认为

result != null

是相同的

!result.contains(null)

它不是。后者可以抛出一个 NPE。

编辑你正在处理'结果'是非空的,即使你知道它是空的,在'else'分支中。你的代码真的没有多大意义。

于 2013-11-09T11:00:29.743 回答