找不到符号方法Android“findviewbyID”

时间:2016-01-03 05:10:10

标签: java android

我是初学者并且在应用程序开发方面磨练我的技能。我目前正在做一个示例,其中List由String Array填充。在最后的步骤中发生绑定,这是找不到方法“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);

2 个答案:

答案 0 :(得分:2)

首先findViewByID应为findViewById(小写d

对于片段,请在onCreateView中设置您的观看次数

// 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;

答案 1 :(得分:0)

更改

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

View view= inflater.inflate(R.layout.fragment_main, container, false);

然后添加您的代码以初始化您的listview,如下所示......

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