无法声明一个按钮,Eclipse不断抛出错误

时间:2013-01-28 15:53:06

标签: android button layout-inflater

我试图放置一个按钮,但我不知道为什么Eclipse不断抛出这个错误“局部变量按钮可能尚未初始化”。我在xml文件“rssfeedadapter_layout”中添加了一个按钮。但我不能在代码中调用它。我试着这个:

RssReaderListAdapter.java

package com.rssfeed.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.rssfeed.R;
import com.rssfeed.helper.RssFeedStructure;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class RssReaderListAdapter extends ArrayAdapter<RssFeedStructure> {

List<RssFeedStructure> imageAndTexts1 =null;
public RssReaderListAdapter(Activity activity, List<RssFeedStructure> imageAndTexts) {
super(activity, 0, imageAndTexts);
imageAndTexts1 = imageAndTexts;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();


View rowView = inflater.inflate(R.layout.rssfeedadapter_layout, null);
TextView title = (TextView) rowView.findViewById(R.id.feed_text);
TextView description = (TextView) rowView.findViewById(R.id.feed_updatetime);
ImageButton button = (ImageButton) button.findViewById(R.id.button_web);//Eclipse keeps throwing error in this line
ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image);
ImageLoader imgLoader;
imgLoader = new ImageLoader(activity);
        Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: " +imageAndTexts1.get(position).getImgLink() +" :: " +imageAndTexts1.get(position).getTitle());
        title.setText(imageAndTexts1.get(position).getTitle());
        description.setText(imageAndTexts1.get(position).getDescription());
        if(imageAndTexts1.get(position).getImgLink() !=null){

            String imageUrl= imageAndTexts1.get(position).getImgLink();
            imgLoader.DisplayImage(imageUrl, imageView);

            }

return rowView;

}

}

2 个答案:

答案 0 :(得分:4)

使用

ImageButton button = (ImageButton) rowView.findViewById(R.id.button_web);

而不是

ImageButton button = (ImageButton) button.findViewById(R.id.button_web);

您需要使用rowViewrssfeedadapter_layout布局而不是按钮

获取初始化按钮

答案 1 :(得分:2)

变化:

button.findViewById(R.id.button_web);

为:

rowView.findViewById(R.id.button_web);