gpt4 book ai didi

java - rootview.findViewById() 在 fragment 文件中不起作用

转载 作者:行者123 更新时间:2023-11-29 07:38:18 24 4
gpt4 key购买 nike

package com.example.mayank.sunshine;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {

private ArrayAdapter<String> mForecastAdapter;

public MainActivityFragment() {
}

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

String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/40",
"Weds - Cloudy - 727/63",
"Thurs - Asteroids - 75/65",
"Fri - Heavy Rain - 65/56",
"Sat - HELP TRAPPED IN WEATHERSTATION - 60/51",
"Sun - Sunny - 80/68"
};

List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));

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

ListView listView = (ListView)rootview.findViewById(R.id.listview_forecast);

listView.setAdapter(mForecastAdapter);

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

运行项目显示编译错误“找不到符号变量rootview”。我是否必须导入任何内容才能运行代码?

附言我正在使用 Android Studio 1.4,默认情况下,它会为 fragment 构建一个单独的 java 文件。

最佳答案

在你的onCreateView()你必须inflate你的rootView你可以这样做->

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

....
return rootView;

关于java - rootview.findViewById() 在 fragment 文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33029845/

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