2013-07-23 1 views
0

이것은 지난 날 저를 괴롭혔습니다. 그래서 계속 mTabHost, onCreate()의 변수에 대한 null 포인터 참조를 얻습니다. 내 주요 활동에서TabHost (Android)에 대한 참조에서 널 포인터 가져 오기

: 여기에 관련 코드의

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    instance = this; 
    setContentView(R.layout.second_activity); 

    // Initializing ViewPager and TabHost objects: 
    mViewPager = new ViewPager(this); 
    mViewPager.setId(R.id.viewpager); 
    setContentView(mViewPager); 
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this,getSupportFragmentManager()); 

} 

그리고 여기 second_activity.xml입니다 :

<?xml version="1.0" encoding="UTF-8"?> 

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom"> 

     <fragment android:name="com.yolostudios.dots.main.login.LoginFragment" 
      android:id="@+id/login_fragment" 
      android:layout_weight="2" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:visibility="gone"/> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0"/> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="8" > 

      <include 
       android:layout_width="fill_parent" 
       android:layout_height="60dip" 
       android:visibility="invisible" 
       android:layout_gravity="bottom" 
       android:focusable="true" 
       layout="@layout/createspotbutton" /> 

     </android.support.v4.view.ViewPager> 

     <!-- Loading header of this UI which is coded separately --> 

     <FrameLayout 
      android:id="@+id/toolbar_container" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" > 

      <include 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="20dp" 
       layout="@layout/toolbar" /> 

     </FrameLayout> 

    </LinearLayout> 

</android.support.v4.app.FragmentTabHost> 

내가 mTabHost.setup (이 프로그램을 실행할 때 실제 예외가 발생, getSupportFragmentManager()) mTabHost는 null입니다.

뭔가 이상한 점은 Support4Demos에서 제공되는 샘플 FragmentTabs도 나를 위해 작동하지 않는다는 것입니다. 나는 이것이 내 안드로이드 지원 v4.jar 또는 내 환경에 문제가 있음을 알려주고 있지만 항아리를 다시 다운로드하고 다시 컴파일하려고했지만 여전히 아무것도 작동하지 않는다는 것을 확신합니다. 컴파일에도 불구하고 내 프로젝트와 런타임시 샘플 프로젝트가 실패합니다. 다른 가능한 환경 문제에 관해서, 나는 그들이 무엇이 될 수 있는지 잘 모르겠습니다.

의견을 보내 주시면 감사하겠습니다.

감사합니다. Andrew.

+1

'android.R.id.tabhost'에서'android.'를 제거하십시오, 나는 Eclipse가 그것을 원한다는 것을 알고 있습니다. – Vikram

+0

Tarsem의 대답에 대한 제 응답을 참조하십시오 –

+0

클래스가'FragmentActivity'를 확장합니까? – Vikram

답변

0

당신이로 변경 어쩌면 경우

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    instance = this; 
    setContentView(R.layout.second_activity); 
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this,getSupportFragmentManager()); 

    // Initializing ViewPager and TabHost objects: 
    mViewPager = new ViewPager(this); 
    mViewPager.setId(R.id.viewpager); 
    setContentView(mViewPager); 


} 
+0

아니요, second_activity.xml에서 알아 차리면 FragmentTabHost에 지정된 ID가 \ @android : id/tabhost이므로 R.id.tabhost가 유효한 R.id. 나는 \ @android : id/tabhost를 간단한 \ @ + id/tabhost로 변경하고 R.id.tabhost로 참조하려고했지만 동일한 nullpointerexception이 발생했습니다. –

+0

가져 오기가 없습니다. 나는 내 자신의 패키지에서 R 클래스를 가져오고있다. –

+0

@aentropy 업데이트 된 답변을 사용해보십시오! –

0

을 시도 : 당신이 내에서의 물체를 발견하기 전에 다른 레이아웃 된 setContentView를 호출하고 있기 때문에

setContentView(R.layout.second_activity); 
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 

// Initializing ViewPager and TabHost objects: 
mViewPager = new ViewPager(this); 
mViewPager.setId(R.id.viewpager); 
setContentView(mViewPager); 
mTabHost.setup(this,getSupportFragmentManager()); 

}

첫 번째 레이아웃 파일?

0

확실치 않습니다. 당신이 android.app.Fragment를 사용하는 경우 Activity을 사용하여 기억 Fragment/FragmentHost/FragmentActivity

를 사용 때 염두에 메시지 이하이 을 유지; android.support.v4.app.Fragment을 사용하는 경우 FragmentActivity을 사용하십시오. android.app.Activityandroid.support.v4.app.Fragment 또는 FragmentHost을 첨부하면 Exception이 던져 지므로 절대로 부착하지 마십시오.