我有问题将我的数据显示到android中的listview当我使用PDO SQL输出我的PHP代码,我搜索但没有像我的代码,我是newbe,请帮助我。 这是我的错误:
6-13 00:51:50.589 21781-21781/com.spp.udi.sppmap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.spp.udi.sppmap, PID: 21781
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spp.udi.sppmap/com.spp.udi.sppmap.Potensi}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at com.spp.udi.sppmap.Potensi.onCreate(Potensi.java:37)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的ListActivity:
package com.spp.udi.sppmap;
import android.app.Fragment;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.app.ListFragment;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class Potensi extends ListActivity {
private Context mContext;
private static final String ID_KECAMATAN = "id_kecamatan";
private static final String NAMA_KECAMATAN = "nama_kecamatan";
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String,String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new fetchDataFromJSON().execute();
}
private class fetchDataFromJSON extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Potensi.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.AmbilJson("http://sppmap.esy.es/android/potensi.php");
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
JSONArray jsonArray = json.getJSONArray("result");
for(int i=0; i<jsonArray.length(); i++){
JSONObject jsonobj = jsonArray.getJSONObject(i);
String id_kecamatan = jsonobj.getString(ID_KECAMATAN);
String nama_kecamatan = jsonobj.getString(NAMA_KECAMATAN);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_kecamatan", id_kecamatan);
map.put("nama_kecamatan", nama_kecamatan);
mylist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.adapter_listview();
}
public void adapter_listview() {
ListAdapter adapter = new SimpleAdapter(mContext, mylist, R.layout.potensi,
new String[] {ID_KECAMATAN, NAMA_KECAMATAN}, new int[] {R.id.id_kecamatan, R.id.nama_kecamatan});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String id_kecamatan = ((TextView) view.findViewById(R.id.id_kecamatan)).getText().toString();
Intent in = new Intent(mContext, DetailPotensi.class);
in.putExtra(ID_KECAMATAN, id_kecamatan);
startActivity(in);
}
});
}
}
}
我希望有人能帮我解决问题,谢谢你。
答案 0 :(得分:0)
作为此行状态
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spp.udi.sppmap/com.spp.udi.sppmap.Potensi}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
这意味着您从URL返回的JSON为null,但我认为HTTP请求永远不会触发。您处理JSON的代码完美无缺。但是在android应用程序中,最好不要在主线程中执行HTTP请求,这会导致崩溃,而是在工作线程上运行它或者使用AsyncTask
创建异步请参阅此link。我以前从不尝试JSONFunction.getJSONFromUrl
发出HTTP请求,但如果我没有错,那么它是一个同步请求进程,所以最好使用其他库来使http请求异步。这里有一些你可以使用的好库