2012-07-06 21 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)

相关问题