0

我在第一次运行时收到两个不同的错误,在第二次运行时收到一个。我以前没见过这个,老实说,我不知道怎么回事。我将展示代码,然后是错误,然后是错误指向的行。

private static final String TAG_SUCCESS = "success";
private static final String TAG_STORE = "store_info";
private static final String TAG_NUMBER = "store_num";
private static final String TAG_ADDRESS = "address";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_ZIP = "zip";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_basic_info, container, false);
        ab = getActivity().getActionBar();

        store_model = new Store_Model();
                ...
}

class Store extends AsyncTask<String, Void, String> {
        private ProgressDialog pDialog;
        JSONParser jParser = new JSONParser();
        JSONArray store_info = null;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading questions. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            // getting JSON string from URL
            String componentName = (String) ab.getSelectedTab().getText();
            companyName = Store_Fragment.model.getcName();
            projectName = Store_Fragment.model.getpName();
            storeNum = store.getText().toString();
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("company", companyName));
            nameValuePairs.add(new BasicNameValuePair("project", projectName));
            nameValuePairs.add(new BasicNameValuePair("component",
                    componentName));
            nameValuePairs.add(new BasicNameValuePair("store", storeNum));

            JSONObject json = jParser.makeHttpRequest(url, "POST",
                    nameValuePairs);
            // Check your log cat for JSON response
            //Log.d("All Store Info: ", json.toString());
            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    Log.v("RESPONSE", "Success!");
                    // products found: getting Array of Store Info
                    store_info = json.getJSONArray(TAG_STORE);

                    // looping through Store Info
                    for (int i = 0; i < store_info.length(); i++) {

                        JSONObject c = store_info.getJSONObject(i);

                        // Storing each JSON item in variable
                        String number = c.getString(TAG_NUMBER);
                        String address = c.getString(TAG_ADDRESS);
                        String city = c.getString(TAG_CITY);
                        String state = c.getString(TAG_STATE);
                        String zip = c.getString(TAG_ZIP);

                        // creating new HashMap
                        map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_NUMBER, number);
                        map.put(TAG_ADDRESS, address);
                        map.put(TAG_CITY, city);
                        map.put(TAG_STATE, state);
                        map.put(TAG_ZIP, zip);                      
                    }

                } else if (success == 0) {
                    // no questions found
                    Log.v("ERROR", "No JSON for you!");
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            error.setText("Invalid Store Number");
                            error.setBackgroundColor(getResources().getColor(
                                    R.color.red_bg));
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String string) {
            // dismiss the dialog
            pDialog.dismiss();

                store_model.setStoreNum(map.get(TAG_NUMBER));
                store_model.setAddress(map.get(TAG_ADDRESS));
                store_model.setCity(map.get(TAG_CITY));
                store_model.setState(map.get(TAG_STATE));
                store_model.setZip(map.get(TAG_ZIP));


            Dialog_Fragment df = new Dialog_Fragment();
            df.show(getFragmentManager(), "store");
        }
    };

所以我第一次运行它时遇到了问题JSON并给了我这个错误。

06-18 17:21:17.870: E/json data(23746): json result 
06-18 17:21:17.870: E/JSON Parser(23746): Error parsing data org.json.JSONException: End of input at character 0 of 

然后,如果我不再编译代码,而只是从我的平板电脑再次启动应用程序,它会进入此屏幕并获得JSON响应,但会给我一个空指针。

这是 JSON 响应和两个 logcat。

JSON

{
  "store_info":
   [
    {
      "store_num":"00607",
      "address":"790 W BROADWAY RD",
      "city":"TEMPE",
      "state":"AZ",
      "zip":"85282"
    }
   ],
   "success":1
}

第一次运行 Logcat

06-18 17:21:17.870: E/json data(23746): json result 
06-18 17:21:17.870: E/JSON Parser(23746): Error parsing data org.json.JSONException: End of input at character 0 of 
06-18 17:21:17.870: W/System.err(23746): java.lang.NullPointerException
06-18 17:21:17.870: W/System.err(23746):    at com.fa.BasicInfo_Fragment$Store.doInBackground(BasicInfo_Fragment.java:105)
06-18 17:21:17.870: W/System.err(23746):    at com.fa.BasicInfo_Fragment$Store.doInBackground(BasicInfo_Fragment.java:1)
06-18 17:21:17.870: W/System.err(23746):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 17:21:17.870: W/System.err(23746):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-18 17:21:17.870: W/System.err(23746):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-18 17:21:17.870: W/System.err(23746):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-18 17:21:17.870: W/System.err(23746):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-18 17:21:17.870: W/System.err(23746):    at java.lang.Thread.run(Thread.java:856)
06-18 17:21:17.890: D/AndroidRuntime(23746): Shutting down VM
06-18 17:21:17.890: W/dalvikvm(23746): threadid=1: thread exiting with uncaught exception (group=0x41093930)
06-18 17:21:17.900: E/AndroidRuntime(23746): FATAL EXCEPTION: main
06-18 17:21:17.900: E/AndroidRuntime(23746): java.lang.NullPointerException
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.fa.BasicInfo_Fragment$Store.onPostExecute(BasicInfo_Fragment.java:156)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.fa.BasicInfo_Fragment$Store.onPostExecute(BasicInfo_Fragment.java:1)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.AsyncTask.finish(AsyncTask.java:631)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.os.Looper.loop(Looper.java:137)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 17:21:17.900: E/AndroidRuntime(23746):    at dalvik.system.NativeStart.main(Native Method)

第二次运行 Logcat

06-18 17:22:19.910: E/json data(23841): json result {"store_info":[{"store_num":"00607","address":"790 W BROADWAY RD","city":"TEMPE","state":"AZ","zip":"85282"}],"success":1}
06-18 17:22:19.910: V/RESPONSE(23841): Success!
06-18 17:22:19.930: D/AndroidRuntime(23841): Shutting down VM
06-18 17:22:19.930: W/dalvikvm(23841): threadid=1: thread exiting with uncaught exception (group=0x41093930)
06-18 17:22:19.940: E/AndroidRuntime(23841): FATAL EXCEPTION: main
06-18 17:22:19.940: E/AndroidRuntime(23841): java.lang.NullPointerException
06-18 17:22:19.940: E/AndroidRuntime(23841):    at com.fa.Dialog_Fragment.onCreateDialog(Dialog_Fragment.java:39)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.os.Handler.handleCallback(Handler.java:725)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.os.Looper.loop(Looper.java:137)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 17:22:19.940: E/AndroidRuntime(23841):    at dalvik.system.NativeStart.main(Native Method)

所以现在我假设这些错误指向的行。

错误日志 1

com.fa.BasicInfo_Fragment$Store.doInBackground(BasicInfo_Fragment.java:105)

int success = json.getInt(TAG_SUCCESS);

com.fa.BasicInfo_Fragment$Store.onPostExecute(BasicInfo_Fragment.java:156)

store_model.setStoreNum(map.get(TAG_NUMBER));

这些错误似乎不言自明。您看不到它是否成功,TAG_SUCCESS因此在尝试时它会崩溃,setStoreNum因为该值为null. 但是,当我第二次运行它时,它就可以工作了吗?这对我来说似乎没有意义。然后它也给了我这个错误。

错误日志 2

com.fa.Dialog_Fragment.onCreateDialog(Dialog_Fragment.java:39)

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder storeD = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    if (BasicInfo_Fragment.store_model != null) {

        mStore = BasicInfo_Fragment.store_model.getStoreNum();
        ...

        store = (TextView) getActivity().findViewById(R.id.store_d);
        ...

        store.setText("Store #: " + mStore); <-- LINE 39
        ...

    }

错误日志 2 似乎正在访问我的对话框片段,然后在尝试创建时崩溃。它从标记的 mStore 变量中得到一个空指针异常,但它应该在它开始之前检查空值,非常令人沮丧。

所以我知道这是一篇很长的帖子,但我试图提供所有信息并使其易于阅读。

请帮我弄清楚为什么这不是我JSON的第一次。然后突然工作,如果我不再次编译但在我的 Dialog_Fragment 的 onCreate 上使用空指针崩溃。

PHP只是为了很好的衡量,所以你可以看到它是如何JSON被创建的

<?php

require 'connect.php';
require 'mysql_queries.php';

if ($query_run = mysql_query($store_info_query)) {
    if (mysql_num_rows($query_run) == NULL) {
        $response["success"] = 0;          
        echo json_encode($response);

    } else {
        $response ['store_info'] = array();
        while ($row = mysql_fetch_assoc($query_run)) {
            $info = array();
            $info['store_num'] = $row['column1'];
            $info ['address'] = $row['column2'];
            $info ['city'] = $row['column3'];   
            $info ['state'] = $row['column4'];
            $info ['zip'] = $row['column5'];        

            array_push($response["store_info"], $info);                     
        }
        // success
        $response["success"] = 1; 
        // echoing JSON response
        echo json_encode($response);        
    } 
} 

?>
4

1 回答 1

1

你用哪个图书馆?请使用特定的android库

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; 
... 
于 2013-06-18T22:18:10.317 回答