gpt4 book ai didi

android - 为什么我收到警告 "variable may not be initialized"

转载 作者:行者123 更新时间:2023-11-29 20:36:27 25 4
gpt4 key购买 nike

我正在尝试使用数组适配器、运行方法和多线程创建 ListView ,但我在以下行中收到警告“变量摘要可能未初始化”:SummaryData summary = summary。得到(位置);。感谢您的帮助,请记住我是 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;
}
}

最佳答案

这里的问题是您的局部变量 SummaryData summary 隐藏了属性 this.summary。尝试将其中一个重命名为其他名称,一切都会像魅力一样工作:)

关于android - 为什么我收到警告 "variable may not be initialized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407192/

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