【问题标题】:Cant declare a button, Eclipse keeps throwing error无法声明按钮,Eclipse 不断抛出错误
【发布时间】:2013-01-28 15:53:06
【问题描述】:

我试图放置一个按钮,但我不知道为什么 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;

}

}

【问题讨论】:

  • 不应该button.findViewById(R.id.button_web)rowView.findViewById(R.id.button_web) 吗?

标签: android button layout-inflater


【解决方案1】:

使用

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

而不是

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

您需要使用rowViewrssfeedadapter_layout 布局而不是按钮中获取初始化按钮

【讨论】:

  • 那些小错误让我头疼……我认为是疲劳。感谢您的回答,这是唯一的问题。
【解决方案2】:

改变:

button.findViewById(R.id.button_web);

到:

rowView.findViewById(R.id.button_web);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多