IllegalArgumentException:找不到片段id的视图

时间:2016-01-17 20:50:24

标签: java android

抛出此异常:

  

引起:java.lang.IllegalArgumentException:找不到ID为0x7f0e006b(com.example.simplegamer003.sunshine.app:id/container)的片段MainActivityFragment {37dff01#1 id = 0x7f0e006b FFTAG}

导致异常的代码:

@Override

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

    mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);

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

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
    listView.setAdapter(mForecastAdapter);

    // We'll call our MainActivity
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // CursorAdapter returns a cursor at the correct position for getItem(), or null
            // if it cannot seek to that position.
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
            if (cursor != null) {
                String locationSetting = Utility.getPreferredLocation(getActivity());
                Intent intent = new Intent(getActivity(), DetailActivity.class)
                        .setData(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
                                locationSetting, cursor.getLong(COL_WEATHER_DATE)
                        ));
                startActivity(intent);
            }
        }
    });
    return rootView;
}

1 个答案:

答案 0 :(得分:1)

试试这个:View rootView = inflater.inflate(R.layout.fragment_main, null, false);而不是View rootView = inflater.inflate(R.layout.fragment_main, container, false);我认为containernull。希望它可以帮到你