ArrayAdapter 和列表视图错误
ArrayAdapter and listview Error
我尝试在以下代码中为列表视图设置适配器。
public class MainActivity extends AppCompatActivity {
JSONArray jsonObject;
ArrayList<String> arrayList= new ArrayList<String>();
static ArrayList<String> articles= new ArrayList<String>();
static ArrayAdapter<String> arrayAdapter;
public class Dwnl extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
URLConnection conn=null;
conn = (HttpURLConnection)url.openConnection();
String contents="";
InputStream in = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data =reader.read();
int counter = 0;
while (data!=-1) {
contents+=(char)data;
data=reader.read();
//ounter++;
}
//contents = in.toString();
jsonObject= new JSONArray(contents);
//Log.i("Sire",String.valueOf(jsonObject.length()));
arrayList = new ArrayList<>();
//arrayList.addAll((Collection<? extends String>) jsonObject);
for(int i = 0 ; i < 3 ; i++) {
try {
//Log.i("URL", "https://hacker-news.firebaseio.com/v0/item/" + jsonObject.get(i) + ".json?print=pretty");
String site = "";
site = "https://hacker-news.firebaseio.com/v0/item/" + jsonObject.get(i) + ".json?print=pretty";
URL Second = new URL(site);
URLConnection newURlCon = (HttpURLConnection) Second.openConnection();
InputStream input = newURlCon.getInputStream();
InputStreamReader secondreader = new InputStreamReader(input);
int pos = secondreader.read();
String all = "";
while (pos != -1) {
all += (char) pos;
pos = secondreader.read();
}
//Log.i("JSON",all);
JSONObject second = new JSONObject(all);
all = "";
articles.add(second.getString("title").toString());
Log.i("JSONSTR",articles.get(i));
//arrayAdapter.notifyDataSetChanged();
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.i("Exp",sw.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Dwnl dwnl = new Dwnl();
try {
dwnl.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty").get();
ListView listView = (ListView) findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,articles);
listView.setAdapter(arrayAdapter);
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.i("Exp",sw.toString());
}
}
}
但是总是出现这个错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
我不知道什么是 null,arraylist 的大小是 3,而且根本没有 null
提前致谢
NullPointerException 由 ListView
而不是数组抛出。
在 onCreate
方法中你应该调用:
setContentView(R.layout.your_layout);
将 your_layout
替换为您的 MainActivity
xml。
Null 是因为你从 .doInBackground() 中 returnnull。您不能从后台线程向适配器设置数据,它会抛出另一个异常。您必须 return 来自 .doInBackground 的结果,然后在 .onPostExecute()
中处理它
我尝试在以下代码中为列表视图设置适配器。
public class MainActivity extends AppCompatActivity {
JSONArray jsonObject;
ArrayList<String> arrayList= new ArrayList<String>();
static ArrayList<String> articles= new ArrayList<String>();
static ArrayAdapter<String> arrayAdapter;
public class Dwnl extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
URLConnection conn=null;
conn = (HttpURLConnection)url.openConnection();
String contents="";
InputStream in = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data =reader.read();
int counter = 0;
while (data!=-1) {
contents+=(char)data;
data=reader.read();
//ounter++;
}
//contents = in.toString();
jsonObject= new JSONArray(contents);
//Log.i("Sire",String.valueOf(jsonObject.length()));
arrayList = new ArrayList<>();
//arrayList.addAll((Collection<? extends String>) jsonObject);
for(int i = 0 ; i < 3 ; i++) {
try {
//Log.i("URL", "https://hacker-news.firebaseio.com/v0/item/" + jsonObject.get(i) + ".json?print=pretty");
String site = "";
site = "https://hacker-news.firebaseio.com/v0/item/" + jsonObject.get(i) + ".json?print=pretty";
URL Second = new URL(site);
URLConnection newURlCon = (HttpURLConnection) Second.openConnection();
InputStream input = newURlCon.getInputStream();
InputStreamReader secondreader = new InputStreamReader(input);
int pos = secondreader.read();
String all = "";
while (pos != -1) {
all += (char) pos;
pos = secondreader.read();
}
//Log.i("JSON",all);
JSONObject second = new JSONObject(all);
all = "";
articles.add(second.getString("title").toString());
Log.i("JSONSTR",articles.get(i));
//arrayAdapter.notifyDataSetChanged();
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.i("Exp",sw.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Dwnl dwnl = new Dwnl();
try {
dwnl.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty").get();
ListView listView = (ListView) findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,articles);
listView.setAdapter(arrayAdapter);
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.i("Exp",sw.toString());
}
}
}
但是总是出现这个错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
我不知道什么是 null,arraylist 的大小是 3,而且根本没有 null
提前致谢
NullPointerException 由 ListView
而不是数组抛出。
在 onCreate
方法中你应该调用:
setContentView(R.layout.your_layout);
将 your_layout
替换为您的 MainActivity
xml。
Null 是因为你从 .doInBackground() 中 returnnull。您不能从后台线程向适配器设置数据,它会抛出另一个异常。您必须 return 来自 .doInBackground 的结果,然后在 .onPostExecute()
中处理它