代码从网页获取JSON对象并将其转换为列表..这里有一个例外正在出现......请帮助我......
process.stdin.setEncoding('utf8');
var input = [];
process.stdin.on('readable', function() {
console.log('What is your name');
var name = process.stdin.read();
if ((name !== null) && (name !== '/n')) {
input.push(name.split('/n')[0]);
console.log(input);
}
console.log('List 3 of your hobbies ?');
var hobbies = process.stdin.read();
});
process.stdin.on('end', function() {
console.log(input);
});
以上程序的日志cat在下面给出... nullpointer 在程序中遇到异常......有人请帮助我..在之前的类似问题中,我无法找到解释.. 原木猫: 通过更改提到的行我得到以下例外:
import android.content.Context;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
public class FeedActivity extends Activity implements OnFeedListener{
ListView listview;
FeedAdapter adapter;
ArrayList<Post> posts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
listview = (ListView)findViewById(R.id.listView);
//Data ->adapter -> ListView
adapter = new FeedAdapter(getApplicationContext(),R.layout.layout_feed_item);
listview.setAdapter(adapter);
FeedTask task = new FeedTask(this);
task.execute("https://public-api.wordpress.com/rest/v1.1/sites/androsiv.wordpress.com/posts/");
}
@Override
public void onFeed(JSONArray array) {
int length = array.length();
for(int i = 0; i<length; i++)
{
JSONObject object = array.optJSONObject(i);
Post post = new Post(object.optString("title"),object.optString("excerpt"),object.optString("featured_image"));
posts = new ArrayList<>();
posts.add(post);
}
adapter.addAll(posts);
adapter.notifyDataSetChanged();
}
public class FeedTask extends AsyncTask<String, Void, JSONArray>
{
private OnFeedListener listener;
public FeedTask(OnFeedListener listener)
{
this.listener = listener;
}
@Override
protected JSONArray doInBackground(String... params) {
String url = params[0];
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.url(url).build();
try {
Response response = client.newCall(request).execute();
String json = response.body().string();
try {
JSONObject object = new JSONObject(json);
JSONArray array = object.optJSONArray("posts");
return array;
}
catch(JSONException e)
{
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONArray array) {
super.onPostExecute(array);
if(null == array)
return;
if(null != listener)
listener.onFeed(array);
}
}
public class FeedAdapter extends ArrayAdapter<Post>
{
private int resource;
public FeedAdapter(Context context, int resource) {
super(context, resource);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(null == convertView)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource,null);
}
//Binding data
Post post = getItem(position);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView desc = (TextView) convertView.findViewById(R.id.description);
title.setText(post.title);
desc.setText(post.description);
return convertView;
}
}
public class Post
{
public String title;
public String description;
public String thumbnail; //url
public Post(String title, String desc, String thumbnail)
{
this.description=desc;
this.thumbnail=thumbnail;
this.title=title;
}
}
}
答案 0 :(得分:0)
问题在于这句话:
adapter.addAll(posts);
目前,在onCreate
方法中,posts
尚未初始化并包含NULL
作为值。你应该
posts
(posts = new ArrayList<Post>()
)addAll(posts)
在posts
为空时可能不会有任何影响,所以完全跳过它。您在[{1}}方法中再次呼叫addAll(posts)
。 旁注:我在onFeed
方法的循环中看到了您使用空数组重新初始化posts
的内容。这没有任何好处。您应该在循环之前初始化您的onFeed()
集合。
答案 1 :(得分:0)
adapter.addAll(公告); 在没有创建初始化的情况下,您尝试添加适配器。这里的帖子arraylist是null,你在asynctask中填充posts数组,这是在上面的行之后启动的。删除上面的行就可以了。