我有2个活动,我想登录,在其他活动中,我想从我的数据库中获取一些数据。
一切正常,除非我想显示数据时出现错误。
我不知道这是如何工作的,有错误日志
06-01 17:33:03.644: E/JSON Parser(555): Error parsing data org.json.JSONException: End of input at character 0 of
06-01 17:33:03.644: D/AndroidRuntime(555): Shutting down VM
06-01 17:33:03.644: W/dalvikvm(555): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-01 17:33:03.654: E/AndroidRuntime(555): FATAL EXCEPTION: main
06-01 17:33:03.654: E/AndroidRuntime(555): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.DashboardActivity}: java.lang.NullPointerException
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.os.Looper.loop(Looper.java:123)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 17:33:03.654: E/AndroidRuntime(555): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 17:33:03.654: E/AndroidRuntime(555): at java.lang.reflect.Method.invoke(Method.java:521)
06-01 17:33:03.654: E/AndroidRuntime(555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-01 17:33:03.654: E/AndroidRuntime(555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-01 17:33:03.654: E/AndroidRuntime(555): at dalvik.system.NativeStart.main(Native Method)
06-01 17:33:03.654: E/AndroidRuntime(555): Caused by: java.lang.NullPointerException
06-01 17:33:03.654: E/AndroidRuntime(555): at com.example.androidhive.DashboardActivity.onCreate(DashboardActivity.java:60)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-01 17:33:03.654: E/AndroidRuntime(555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-01 17:33:03.654: E/AndroidRuntime(555): ... 11 more
我的UserFunctions.java中有一个函数strijk()
public JSONObject strijk(String email){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", strijk_tag));
params.add(new BasicNameValuePair("email", email));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
// Log.e("JSON", json.toString());
return json;
}
在我的第二个活动中,我想显示我从json获得的数据。
UserFunctions userFunction = new UserFunctions();
JSONObject strijk = userFunction.strijk(name);
//check for strijk
try {
if (strijk.getString(KEY_SUCCESS) != null) {
error.setText("");
String res =strijk.getString(KEY_SUCCESS);
if (Integer.parseInt(res)==1){
String strijkid = strijk.getString(KEY_STRIJK);
String klaar = strijk.getString(KEY_KLAAR);
kla =(TextView) findViewById(R.id.klaar);
strijkd = (TextView) findViewById(R.id.strijkid);
kla.setText(klaar);
strijkd.setText(strijkid);
}
}
} catch (JSONException e) {
Log.e("log_tag","Error in Display!" + e.toString());;
}
这是我的JSON PARSER
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是为了我的学校,我还没有那么擅长Android。