android gridview总是为null

时间:2012-04-15 09:10:30

标签: android android-layout

在我的活动中我有:

DrawView,这是一个自定义相对布局。

此RelativeLayout应存在自定义导航栏和自定义GridView

Custom NavigationBar在此处添加到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始终为null,为什么会这样!?

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);       
}

3 个答案:

答案 0 :(得分: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()之前添加它。

答案 1 :(得分:2)

您有2个选项

  1. 您使用findViewById。您正在寻找的视图必须使用setContentView()“在屏幕上”。没有别的方法,这就是函数findViewById的用途。
  2. 如果您不能使用setContentView(),因为您想要以编程方式设置此视图,并且由于某种原因它不是您要设置的XML,则必须 INFLATE 视图,然后用它做你的魔法,然后你可以随意使用setContentView()。如何执行此操作的示例很简单,但请查看the manual和一些random example

答案 2 :(得分: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>