2012-07-06 66 views
0

的子類空我有一個SettingsActivity是PreferenceActivity的子類在我的圖書館項目在返回的LinearLayout是PreferenceActivity

的onCreate方法看起來像這樣

addPreferencesFromResource(R.xml.preferences); 
setContentView(R.layout.main); 

的preference.xml文件有結構是怎樣的元素PreferenceScreen - > PreferenceCategory - >首選項 我主要的main.xml貌似這個

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 


    <!--Listview will be replaced by preferences --> 

    <ListView android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

</LinearLayout> 

上面的類SettingsActivity是另一個項目的子類,看起來像這樣

public class SettingsActivityFree extends SettingsActivity 
{ 
     private AdView adView; 

     @Override 
    protected void onCreate(Bundle savedInstanceState) 
     {   
    super.onCreate(savedInstanceState); 

      //... 

      // layout returned will be null .. 
      LinearLayout layout = (LinearLayout)findViewById(R.layout.main); 

      // Add the adView to it 
      layout.addView(adView); 
     } 

問題是我想從父類得到的LinearLayout,這樣我可以在裏面加我的東西,但由於某些原因,它返回空,我有LinearLyout在SettingsActivity類的原因是因爲我希望把一些廣告等在喜好的頂部,並沒有把廣告特定代碼在庫項目

請告知,如果我在這裏失去了一些東西,

謝謝

回答

2

首先,你需要給你的LinearLayout的id使用android:id="@+id/whatever"。然後您可以使用findViewById(R.id.whatever)進行搜索。

+1

謝謝我的工作,是我的錯誤深夜工作:) – Ahmed 2012-07-06 05:48:56

0

你應該真的檢查開發者網站。你做得不正確。在ListView和ListActivity上查找文檔。在使用它之前瞭解它是如何工作的。

另外,作爲一個暗示:

你不這樣做(LinearLayout)findViewById(R.layout.main);

當檢索一個觀點,你會發現它在R.id.(whatever)R.layout.(whatever)