我在第一次运行时收到两个不同的错误,一个在第二次运行。我以前没有见过这个,老实说,我不知道该怎么回事。我将显示代码,然后显示错误,然后显示错误指向的行。
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响应和两个logcats。
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似乎进入我的Dialog Fragment,然后在尝试创建时崩溃。它正在从标记的mStore变量中获取一个空指针异常,但它应该在它开始之前检查null,非常令人沮丧。
所以我知道这是一个相当冗长的帖子,但我试图提供所有信息并使其易于阅读。
请帮我弄清楚为什么第一次没有得到JSON
。如果我不再编译但是在onCreate上使用空指针在我的Dialog_Fragment上崩溃,那么突然工作。
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);
}
}
?>
答案 0 :(得分:1)
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
...