我想從服務器調用JSON數組JSON的網址不工作
這條線工作正常,我
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php");
但是當我嘗試使用我的sharedprefences類來獲取它不工作,CME與力停下來,這裏是代碼
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
+ Test.this.myprefs.getEmail());
在日食它不指向任何錯誤我的意思是缺少變種減速,但它不工作。任何建議?
以下是我在logcat的
05-02 19:49:53.569: ERROR/AndroidRuntime(20721): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.os.Looper.loop(Looper.java:123)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at dalvik.system.NativeStart.main(Native Method)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): Caused by: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): ... 11 more
05-02 19:49:53.778: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:49:58.048: ERROR/PackageInstallationReceiver(248): Remove /data/local/tmp/com.droidnova.android.howto.optionmenu.apk Fail!
05-02 19:51:41.418: ERROR/AndroidRuntime(20746): ERROR: thread attach failed
05-02 19:51:42.818: ERROR/AndroidRuntime(20754): ERROR: thread attach failed
05-02 19:51:43.438: ERROR/AndroidRuntime(20762): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.os.Looper.loop(Looper.java:123)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at dalvik.system.NativeStart.main(Native Method)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): Caused by: java.lang.NullPointerException
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): ... 11 more
05-02 19:51:45.178: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:51:54.428: ERROR/AndroidRuntime(20772): ERROR: thread attach failed
05-02 19:51:55.818: ERROR/AndroidRuntime(20780): ERROR: thread attach failed
05-02 19:51:56.528: ERROR/AndroidRuntime(20788): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.os.Looper.loop(Looper.java:123)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at dalvik.system.NativeStart.main(Native Method)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): Caused by: java.lang.NullPointerException
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): ... 11 more
05-02 19:51:58.658: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:55:31.438: ERROR/AndroidRuntime(20809): ERROR: thread attach failed
05-02 19:55:38.148: ERROR/AndroidRuntime(20820): ERROR: thread attach failed
05-02 19:55:41.638: ERROR/PackageInstallationReceiver(248): Remove /data/local/tmp/com.droidnova.android.howto.optionmenu.apk Fail!
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:29.648: ERROR/wpa_supplicant(139): prepare_filter_struct: type=5
05-02 20:15:32.728: ERROR/wpa_supplicant(139): prepare_filter_struct: type=5
更多更詳細的我在這裏看到的是Test類
public class Test extends ListActivity {
Prefs myprefs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
+ Test.this.myprefs.getEmail());
try{
JSONArray earthquakes = json.getJSONArray("earthquakes");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("name"));
map.put("password", "Magnitude: " + e.getString("password"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,
new String[] { "name", "magnitude" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
,這裏是共享的喜好類
package com.droidnova.android.howto.optionmenu;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class Prefs {
private SharedPreferences _prefs = null;
private Editor _editor = null;
private String _useremailaddress = "Unknown";
private String _serverurl = "http://chinar.gofreeserve.com/db.php";
public Prefs(Context context) {
this._prefs = context.getSharedPreferences("PREFS_PRIVATE", Context.MODE_PRIVATE);
this._editor = this._prefs.edit();
}
public String getValue(String key, String defaultvalue) {
if (this._prefs == null) {
return "Unknown";
}
return this._prefs.getString(key, defaultvalue);
}
public void setValue(String key, String value) {
if (this._editor == null) {
return;
}
this._editor.putString(key, value);
}
public String getEmail() {
if (this._prefs == null) {
return "Unknown";
}
this._useremailaddress = this._prefs.getString("emailaddress", "noidea");
return this._useremailaddress;
}
public String getServer() {
if (this._prefs == null) {
return "http://chinar.gofreeserve.com";
}
this._serverurl = this._prefs.getString("serverurl", "http://chinar.gofreeserve.com/");
return this._serverurl;
}
public void setEmail(String newemail) {
if (this._editor == null) {
return;
}
this._editor.putString("emailaddress", newemail);
}
public void setServer(String serverurl) {
if (this._editor == null) {
return;
}
this._editor.putString("serverurl", serverurl);
}
public void save() {
if (this._editor == null) {
return;
}
this._editor.commit();
}
}
FC中會出現什麼樣的異常?請分享你的堆棧跟蹤。還有,'myprefs'成員的初始化。謝謝! – rekaszeru 2011-05-02 18:09:44
你得到什麼樣的錯誤?請發佈相關部分的logcat。 – 2011-05-02 18:10:47
是Test.this.myprefs.getEmail()url編碼的? – 2011-05-02 18:12:54