2014-06-09 2 views
0

그래서 다음과 같은 코드가 있습니다.fragment_main의 id 태그에 액세스 할 수 없습니다.

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.layout.fragment_main 대신에 R.layout.activity_main (setContentView가 R.layout.activity_main으로 설정했기 때문에)이라는 것입니다. 그러나 setContentView에 activity_main을 사용해야합니다. 이 문제를 해결하는 방법에 대한 아이디어가 있습니까? 대부분의 온라인 장소는 이것이 문제라고 말했지만 답변을주지 않았습니다.

감사합니다.

답변

0

예 활동에서 사용하지 않은 다른 레이아웃의보기에 액세스하고 있으므로 null입니다.

솔루션 :

1 : 당신은 activity_main

또는

이 당신에 fragment_main에서의 ListView를 전송할 수 있습니다 : 당신은 단편을 시작할 수에 대한 배치로 fragment_main 설정 그리고 조각 모음에서 ListView를 인스턴스화 할 수 있습니다.