だから私は次のコードを持っています。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
savedSearches = getSharedPreferences(SEARCHES, MODE_PRIVATE);
// store the saved tags in an ArrayList then sort them
tags = new ArrayList<String>(savedSearches.getAll().keySet());
Collections.sort(tags, String.CASE_INSENSITIVE_ORDER);
// create ArrayAdapter and use it to bind tags to the ListView
adapter = new ArrayAdapter<String>(this, R.layout.list_item, tags);
System.out.println(R.id.list);
ListView lv = (ListView) findViewById(R.id.list);
lv.setAdapter(adapter);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
問題は、インスタンス化した後に lv が null になることです。R.id.list は期待どおりに int を返すため、R.id.list が存在することはわかっています。問題は、R.id.listがR.layout.activity_mainではなくR.layout.fragment_mainにあることだと思います(setContentViewがR.layout.activity_mainに設定しているため)。ただし、setContentView には activity_main を使用する必要があります。これを修正する方法についてのアイデアはありますか? オンラインのほとんどの場所は、これが問題であると述べただけですが、答えはありませんでした.
ありがとう。