我正在尝试从url解析JSON,但它无法正常工作..
错误在第72行第42行
MainActivity$ReadJSON.onPostExecute(MainActivity.java:71)
at com.ac.app.MainActivity$ReadJSON.onPostExecute(MainActivity.java:42)
这是代码
public class MainActivity extends AppCompatActivity {
ArrayList<Video> arrayList;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listView);
runOnUiThread(new Runnable() {
@Override
public void run() {
new ReadJSON().execute("http://example.com/api/data");
}
});
}
class ReadJSON extends AsyncTask<String, String, String>//line no 42
{
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
try {
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("videos");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject videoObject = jsonArray.getJSONObject(i);
arrayList.add(new Video(
videoObject.getString("picture"),
videoObject.getString("title"),
videoObject.getString("pageid")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter); //line no 71
}
}
java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.ListView.setAdapter(android.widget.ListAdapter)&#39;在...上 null对象引用
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ac.app.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:0)
这意味着它无法从您的xml文件中找到listview,从而抛出nullpointer异常。检查你的xml文件
答案 1 :(得分:0)
对于此错误,将有两种可能性
1)列表没有在xml中正确定义(可能是listview id如android:id =“@ android:id / list”表示不是android:id =“@ + id / listView”)
2)arrayList将为null,因为可能未在arrayList中添加Video。
所以在onPostExecute:
的开头放在下面的行arrayList = new ArrayList<Video>();
答案 2 :(得分:0)
我试试这个:
public class MainActivity extends AppCompatActivity {
ArrayList<Video> arrayList;
ListView lv;
CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listView);
new ReadJSON().execute("http://example.com/api/data");
}
class ReadJSON extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
try {
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("videos");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject videoObject = jsonArray.getJSONObject(i);
arrayList.add(new Video(
videoObject.getString("picture"),
videoObject.getString("title"),
videoObject.getString("pageid")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new CustomListAdapter(getApplicationContext(), R.layout.custom_list_layout, arrayList);
lv.setAdapter(adapter);
}
}