2012-04-15 43 views
0

在我的活動中我有:android gridview始終爲空

DrawView這是一個自定義的相對佈局。

這RelativeLayout的應該存在一個自定義導航欄和自定義的GridView的

自定義導航欄在這裏添加到drawView函數:

final LayoutInflater factory = getLayoutInflater(); 
     final View textEntryView = factory.inflate(R.layout.multiplechoice, null); 
     com.lernapp.src.Views.NavigationBarTest navigation = (com.lernapp.src.Views.NavigationBarTest)textEntryView.findViewById(R.id.navigationbar); 
     RelativeLayout parent = (RelativeLayout)navigation.getParent(); 
     parent.removeAllViews(); 

     drawView.addView(navigation); 

我想做的事與GridView控件相似的東西..

我不明白,我的CustomGridView總是空,爲什麼這樣!?

MyCustomGridView grid = (MyCustomGridView)findViewById(R.id.gridview); 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.lernapp.src.Views.MyCustomGridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:cacheColorHint="#00000000" 
     android:listSelector="@android:color/transparent" 
     android:numColumns="auto_fit" 
     android:columnWidth="160dp"/> 
</LinearLayout> 

CustomGridView:

public class MyCustomGridView extends GridView implements OnItemClickListener{ 
     private Listener mListener; 

     public MyCustomGridView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setOnItemClickListener(this); 
     } 

     public void onItemClick(AdapterView parent, View v, int position, long id) { 
     if (mListener != null) { 
      mListener.onClick(position); 
     } 
     } 

     public void setListener(Listener l){ 
     mListener = l; 
     } 

     public interface Listener{ 
     void onClick(int position); 
     } 

    } 

編輯:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    int height = this.getWindowManager().getDefaultDisplay().getHeight(); 
    int width = this.getWindowManager().getDefaultDisplay().getWidth(); 

    MyCustomGridView grid = (MyCustomGridView)findViewById(R.id.gridview); 

    DrawView drawView = new DrawView(this, dragFieldText, targetFieldText, height, width, grid); 

    final LayoutInflater factory = getLayoutInflater(); 
    final View textEntryView = factory.inflate(R.layout.multiplechoice, null); 
    com.lernapp.src.Views.NavigationBarTest navigation = (com.lernapp.src.Views.NavigationBarTest)textEntryView.findViewById(R.id.navigationbar); 
    RelativeLayout parent = (RelativeLayout)navigation.getParent(); 
    parent.removeAllViews(); 

    drawView.addView(navigation); 

    setContentView(drawView);  
} 
+1

您需要setContentView您的佈局在調用findViewById之前。你能發佈活動代碼嗎? – Heinrisch 2012-04-15 09:16:35

+0

添加爲主帖子中的編輯 – krackmoe 2012-04-15 09:19:52

+0

但是,我怎麼能在這裏做到這一點? 因爲我有一個自定義DrawView是一個自定義的相對佈局,並且在這個佈局中我想添加GridView ... – krackmoe 2012-04-15 09:22:34

回答

3

此行添加到您的onCreate一切都會好:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.the_name_of_the_layout_for_this_activity); 

而且要真正清楚,其

setContentView(R.layout.the_name_of_the_layout_for_this_activity); 

您應該添加到的onCreate,必須在您嘗試並執行findViewById()之前添加。

+0

但我沒有這個活動的佈局,佈局存在的DrawView,這是一個相關佈局,其中一切都構造 – krackmoe 2012-04-15 09:30:25

+0

你能否更新問題,以清楚地顯示什麼樣的佈局使用和他們被稱爲:) – erbsman 2012-04-15 09:32:27

+0

好吧我更新它 – krackmoe 2012-04-15 09:35:19

0

正如Heinrisch說,你需要使用findViewById之前調用的setContentView()。 爲什麼不把自定義相對佈局添加到您的佈局xml?通常你不需要自己膨脹視圖。

<com.lernapp.src.Views.DrawView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <com.lernapp.src.Views.MyCustomGridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:cacheColorHint="#00000000" 
     android:columnWidth="160dp" 
     android:listSelector="@android:color/transparent" 
     android:numColumns="auto_fit" /> 
</com.lernapp.src.Views.DrawView> 
+0

添加到我的layout.xml中意味着什麼? – krackmoe 2012-04-15 09:26:13

2

您有2個選項

  1. 您使用findViewById。您正在尋找的視圖必須是「在屏幕上」使用setContentView()。這是沒有其他的方式,這就是功能findViewById是用於。
  2. 如果你不能使用setContentView(),因爲你想以編程方式設置這個視圖,並且它不是在你想設置的XML中出於某種原因,你必須INFLATE的視圖,然後用它做你的魔術,然後你可以根據需要使用setContentView()。如何做到這一點很容易,但檢查出the manual和一些random example