我仍然是Android开发的新手,现在我正在尝试开发一个简单的应用程序来研究更多的android开发。我也尝试使用JSON Parser,但是在显示数据时我有点问题。
我的调试器出错:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.banjir.info.infobanjir, PID: 1847
java.lang.NullPointerException
at com.banjir.info.infobanjir.perak_activity$GetData.onPostExecute(perak_activity.java:146)
at com.banjir.info.infobanjir.perak_activity$GetData.onPostExecute(perak_activity.java:40)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
我已经看过第146和第40行,但我找不到问题。在问这里之前我已经在谷歌做了一些研究,因为我不理解这个概念,我无法解决这个问题。
这是我的活动代码:
package com.banjir.info.infobanjir;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by USER on 15/2/2017.
*/
public class perak_activity extends AppCompatActivity {
private String TAG = perak_activity.class.getSimpleName();
private ListView lv;
ArrayList<HashMap<String, String>> banjirList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.perak_layout);
banjirList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetData().execute();
}
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(perak_activity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://banjir-api.herokuapp.com/api/v1/reports.json";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray data = jsonObj.getJSONArray("data");
// looping through All data
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String id = c.getString("id");
String paras_air = c.getString("paras_air");
String jenis_jalan = c.getString("jenis_jalan");
String section = c.getString("section");
String no_laluan = c.getString("no_laluan");
String nama_laluan = c.getString("nama_laluan");
String nama_jalan = c.getString("nama_jalan");
String daerah = c.getString("daerah");
String negeri = c.getString("negeri");
String dikemaskini = c.getString("dikemaskini");
String status = c.getString("status");
String latitude = c.getString("latitude");
String longitude = c.getString("longitude");
String google_maps_url = c.getString("google_maps_url");
// tmp hash map for single contact
HashMap<String, String> data1 = new HashMap<>();
// adding each child node to HashMap key => value
data1.put("id", id);
data1.put("paras_air", paras_air);
data1.put("jenis_jalan", jenis_jalan);
data1.put("section", section);
data1.put("no_laluan", no_laluan);
data1.put("nama_laluan", nama_laluan);
data1.put("nama_jalan", nama_jalan);
data1.put("daerah", daerah);
data1.put("negeri", negeri);
data1.put("dikemaskini", dikemaskini);
data1.put("status", status);
data1.put("latitude", latitude);
data1.put("longitude", longitude);
data1.put("google_maps_url", google_maps_url);
// adding contact to contact list
banjirList.add(data1);
}
// Getting JSON Object About
JSONObject about = jsonObj.getJSONObject("about");
String github = about.getString("github");
String source = about.getString("source");
String created_by = about.getString("created_by");
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(perak_activity.this, banjirList,
R.layout.list_item, new String[]{ "paras_air","nama_laluan", "daerah", "status"},
new int[]{R.id.paras_air, R.id.nama_laluan, R.id.daerah, R.id.status});
lv.setAdapter(adapter);
}
}
}
我遵循了TutorialPoint的教程JSON Parse,我试图更改api url以探索更多内容。但是我得到了一个类似的错误,原始编码来自教程工作。
答案 0 :(得分:0)
确保R.layout.perak_layout
的{{1}}标识为ListView
。如果它没有标识为R.id.list
的视图,则您的R.id.list
将不会被初始化,并且它将为空,因此ListView lv
上的NullPointerException