gpt4 book ai didi

java - 找不到符号方法Android "findviewbyID"

转载 作者:行者123 更新时间:2023-12-02 04:03:54 26 4
gpt4 key购买 nike

我是一名初学者,正在磨练我的应用程序开发技能。我目前正在做一个示例,其中列表由字符串数组填充。在最后的步骤中进行绑定(bind),这是找不到方法“findviewbyid”的地方,并且在构建后显示为红色。有人可以指导我吗?

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

ArrayList<String> weather = new ArrayList<String>();
weather.add("Today-Sunny-80/23");
weather.add("Tomorrow-Sunny-83/23");
weather.add("Fri-Sunny-80/23");
weather.add("Sat-Rainy-80/23");
weather.add("Sun-Sunny-80/23");
weather.add("Mon-Foggy-80/23");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weather);

ListView listView = (ListView) findViewByID(R.id.listView_Forecast);
listView.setAdapter(adapter);

return inflater.inflate(R.layout.fragment_main, container, false);

最佳答案

首先findViewByID应该是findViewById(小写d)

对于 fragment ,请在 onCreateView 中设置 View ,如下所示

// Call the layout inflater first
View view = inflater.inflate(R.layout.fragment_main, container, false);
// Initialize your views
ListView listView = (ListView) view.findViewById(R.id.listView_Forecast);
listView.setAdapter(adapter);

// Do the rest of your code
...
return view;

关于java - 找不到符号方法Android "findviewbyID",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34573533/

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