错误:找不到符号方法setContentView(int)

时间:2017-08-15 20:00:19

标签: android android-activity fragment oncreate

我是Android Studio中的新手。我遇到了setContentView的问题。 我知道它在Fragment中不起作用。

任何人都知道如何解决这个问题?

错误:

  

错误:找不到符号方法setContentView(int)

代码:

public class Tab1Fragment extends Fragment{
    private static final String TAG = "Tab1Fragment";


    Toolbar mToolbar;
    ListView mListView;

    String[] countryNames = {"Australia", "Brazil", "China", "France", "Germany", "India", "Ireland", "Italy"
            , "Mexico", "Poland", "Russia", "Spain", "US"};
    int[] countryFlags = {R.drawable.flag_australia,
            R.drawable.flag_brazil,
            R.drawable.flag_china,
            R.drawable.flag_france,
            R.drawable.flag_germany,
            R.drawable.flag_india,
            R.drawable.flag_ireland,
            R.drawable.flag_italy,
            R.drawable.flag_maxico,
            R.drawable.flag_poland,
            R.drawable.flag_russia,
            R.drawable.flag_spain,
            R.drawable.flag_us};


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment1_layout);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("My Application");
        mListView = (ListView) findViewById(R.id.listview);
        MyAdapter myAdapter = new MyAdapter(Tab1Fragment.this, countryNames, countryFlags);
        mListView.setAdapter(myAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent mIntent = new Intent(Tab1Fragment.this, DetailActivity.class);
                mIntent.putExtra("countryName", countryNames[i]);
                mIntent.putExtra("countryFlag", countryFlags[i]);
                startActivity(mIntent);
            }
        });
    }

    private Button btnTEST;
}

1 个答案:

答案 0 :(得分:1)

在片段中,您不会使布局膨胀setContentView  代码可能看起来像这样

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment1_layout, container, false);

      // Put your fragment code here

        return rootView;
    }