我有一个名为 ResultActivity 的类,它显示了place'info列表。此活动请求服务器并接收json对象。但我的活动不起作用,我发现了这个错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shameal/com.example.shameal.ResultActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.shameal.controller.AppController.getImageLoader()' on a null object reference
ResultActivity
public class ResultActivity extends ListActivity{
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
...
listView = (ListView) findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
...
}
FeedListAdapter
public class FeedListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
}
@Override
public int getCount() {
return feedItems.size();
}
@Override
public Object getItem(int location) {
return feedItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.feed_item, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView address = (TextView) convertView
.findViewById(R.id.address);
TextView description = (TextView) convertView
.findViewById(R.id.description);
TextView rate = (TextView) convertView.findViewById(R.id.rate);
NetworkImageView image = (NetworkImageView) convertView
.findViewById(R.id.image);
FeedImageView feedImageView = (FeedImageView) convertView
.findViewById(R.id.feedImage1);
FeedItem item = feedItems.get(position);
name.setText(item.getName());
// Converting timestamp into x ago format
address.setText(item.getAddress());
// Chcek for empty status message
if (!TextUtils.isEmpty(item.getDescription())) {
description.setText(item.getDescription());
description.setVisibility(View.VISIBLE);
} else {
// status is empty, remove from view
description.setVisibility(View.GONE);
}
rate.setText(item.getRate());
image.setImageUrl(item.getImage(), imageLoader);
// Feed image
if (item.getMenu() != null || item.getMenu() != "0") {
feedImageView.setImageUrl(item.getMenu(), imageLoader);
feedImageView.setVisibility(View.VISIBLE);
feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
} else {
feedImageView.setVisibility(View.GONE);
}
return convertView;
}
}
我认为此行中的错误: listAdapter = new FeedListAdapter(this,feedItems); 但我找不到解决方法来修复它。
感谢您的帮助!
已编辑:我遵循了本教程:http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/。我只是使用本教程的代码,但我的应用程序不起作用。
答案 0 :(得分:4)
在清单
中添加一行<application
android:name=".AppController" //this one
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
答案 1 :(得分:0)
似乎AppController.getInstance()
返回null
所以当您执行getImageLoader()
时,它会失败