gpt4 book ai didi

java.lang.NullPointerException : Attempt to invoke virtual method . .. 空对象引用

转载 作者:太空狗 更新时间:2023-10-29 16:35:35 26 4
gpt4 key购买 nike

我有一个名为 ResultActivity 的类,它显示地点信息列表。此 Activity 向服务器请求并接收一个 json 对象。但是我的 Activity 不起作用,我发现了这个错误

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

结果 Activity

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/ .我只是使用本教程的代码,但我的应用程序无法运行。

最佳答案

在 list 中添加一行

<application
android:name=".AppController" //this one
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

关于java.lang.NullPointerException : Attempt to invoke virtual method . .. 空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29868527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com