2015-04-25 70 views
2

我有一個類叫做ResultActivity它顯示place'info列表。此活動請求服務器並接收json對象。但是,我的活動不工作,我發現這個錯誤(已解決)java.lang.NullPointerException:試圖調用虛擬方法...在空對象引用

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; 
} 

}

我認爲錯誤在這line:listAdapter = new FeedListAdapter(this,feedItems);但我找不到解決方案來解決它。

感謝您的幫助!

編輯:我遵循本教程:http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/。我只是使用本教程的代碼,但我的應用程序不起作用。

+0

問題是'FeedListAdapter'類,所以請後'有問題 –

+0

FeedListAdapter'類代碼我編輯了我的問題 – nchv

+0

您正在'FeedListAdapter'類中使用'ImageLoader'。這個空指針異常是在您使用該ImageLoader時發生的,而沒有像'ImageLoader.getInstance()'那樣初始化它。 請發佈您的'FeedListAdapter',以便我可以給你準確的答案。 –

回答

2

添加一行來體現

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

看來AppController.getInstance()回報null所以當你執行getImageLoader(),它失敗

相關問題