为什么我收到警告"变量可能无法初始化"

时间:2015-07-14 12:54:02

标签: android multithreading listview android-fragments android-arrayadapter

我正在尝试使用数组适配器,运行方法和多线程创建列表视图,但我收到警告"变量摘要可能无法初始化"在以下行:SummaryData summary = summary.get(position);。感谢您的帮助,请记住,我是Android开发的新手。

我的MainActivity看起来像这样:

public class MainActivity extends FragmentActivity implements OnClickListener{
...
    public void updateResponse() {
        runOnUiThread(new Runnable() {


            @Override
            public void run() {
                StorylineAdapter adapter = new StorylineAdapter(MainActivity.this, R.layout.item_storyline, summary);
                mEditTextResponse.setAdapter(adapter);
}

我的ArrayAdapter看起来像这样:

public class StorylineAdapter extends ArrayAdapter<SummaryData>{
    private Context context;
    private Runnable runnable;
    private ArrayList<SummaryData> summary;

    public StorylineAdapter(Context context, int resource, ArrayList<SummaryData> objects) {
        super(context, resource, objects);
        this.context = context;
        this.runnable = runnable;
        this.summary = objects;
    }



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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_storyline, parent, false);


        SummaryData summary = summary.get(position);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(summary.getActivity());

        return view;
    }
}

1 个答案:

答案 0 :(得分:2)

这里的问题是您的本地变量SummaryData summary正在隐藏属性this.summary。尝试将其中一个重命名为其他内容,一切都会像魅力一样:)