【问题标题】:rootview.findViewById() is not working in fragment filerootview.findViewById() 在片段文件中不起作用
【发布时间】:2016-01-06 21:43:37
【问题描述】:
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,默认情况下,它会为片段构建一个单独的 java 文件。

【问题讨论】:

  • 为什么不使用 getView() ?
  • 在复制粘贴代码之前,请尝试了解它是什么。它显示“找不到符号变量”,因为您的代码中没有这样的变量/字段。
  • rootView 在调用它之前甚至没有被分配。你在那里计划什么,从一个空实体中提取一个列表视图,然后在片段上扩展你自己的布局?简而言之,onCreate 旨在返回您需要显示的视图。先膨胀,然后从rootView中提取列表视图,然后返回rootView
  • @RandykaYudhistira 我是 android 开发新手,我对 getView() 没有任何了解
  • @stack_ved 感谢您的解释。作为初学者,我无法正确理解错误。

标签: java android android-fragments android-studio


【解决方案1】:

您必须先获取您的 rootview,然后才能从中获取内容。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentlayout, container, false); 
    linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout); return rootView; 
} 

【讨论】:

    【解决方案2】:

    这是错误,因为您没有在 rootview 变量中膨胀您的布局。首先声明一个 View 类型变量说 rootView 作为本地或全局然后在 rootView 中膨胀你的布局,然后使用 rootView.findViewById(R.id.listview_forecast); 这是您正在使用的完整代码

    public class MainActivityFragment extends Fragment {
        private ArrayAdapter<String> mForecastAdapter;
        private View rootView;
    
        public MainActivityFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootView=inflater.inflate(R.layout.fragment_main, container, false);
    
            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);
    
            return rootView;
        }
    }
    

    只需用这个替换你的代码就可以了:-)

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 太好了,您可以接受它,这样它就可以清除未回答的问题队列
      • 抱歉。这是我在这里的第一个问题,所以不知道。
      猜你喜欢
      • 2013-05-11
      • 2017-09-03
      • 2020-02-03
      • 2014-10-02
      • 2021-01-03
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多