2013-07-15 68 views
5

我必須開發一個android應用程序。在android中隱藏包含的佈局

我已使用include標記創建了一個使用另一個佈局文件的佈局文件。

<include 
    android:id="@+id/footer" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    layout="@layout/footer_tabs" /> 
    <include 
    android:id="@+id/footer1" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    layout="@layout/footertabs" /> 

我想顯示包含的佈局時,響應爲空,否則我想隱藏佈局,並顯示其他。以下是我迄今爲止:

footertabs = (RelativeLayout) findViewById(R.id.footertab); 
footer_tabs = (RelativeLayout) findViewById(R.id.footer_tab); 

if (Constants.response==null) { 
    footertabs.setVisibility(View.VISIBLE); 
    footer_tabs.setVisibility(View.GONE); 
} 
else 
{ 
    footertabs.setVisibility(View.GONE); 
    footer_tabs.setVisibility(View.VISIBLE); 
} 

但我發現了以下錯誤:

07-15 17:19:09.893: E/AndroidRuntime(15143): Caused by: java.lang.NullPointerException 
07-15 17:19:09.893: E/AndroidRuntime(15143): at com.example.androidbestinuk.HomePage.onCreate(HomePage.java:56) 

請幫我調試這個錯誤。

回答

5

你應該改變

footertabs = (RelativeLayout) findViewById(R.id.footertab); 
footer_tabs = (RelativeLayout) findViewById(R.id.footer_tab); 

footertabs = (RelativeLayout) findViewById(R.id.footer); 
footer_tabs = (RelativeLayout) findViewById(R.id.footer1); 
1

那麼在我看來,你使用的是錯誤的ID。你在某個地方得到一個空指針(我不知道在哪裏,因爲沒有行號),但我看到你的xml中有你的id,footerfooter1,但是在你的代碼中你試圖找到id爲footertab ,和footer_tab。你應該讓這些ID匹配。