0

In my Activity i have:

DrawView which is a Custom Relative Layout.

This RelativeLayout should exist of a Custom Navigationbar and a Custom GridView

The Custom NavigationBar is added here to the 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);

i wanted to do something similar with the GridView..

I dont get it, my CustomGridView is always null, why is this!?

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

    }  

EDIT:

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);       
}
krackmoe
  • 1,743
  • 7
  • 29
  • 53

3 Answers3

3

Add this line to your onCreate and all will be good:

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

And to be really clear, its

    setContentView(R.layout.the_name_of_the_layout_for_this_activity);

You should add to the onCreate, and it must be added BEFORE you try and do findViewById().

Carl-Emil Kjellstrand
  • 1,233
  • 1
  • 10
  • 17
2

You have 2 options

  1. you use findViewById. The view you're looking for MUST be "on the screen" by having used setContentView(). THere's no other way around it, that's what the function findViewById is for.
  2. If you cannot use setContentView() because you want to programatically set this view and it is not in an XML you want to set for some reason, you have to INFLATE the view and then do your magic with it, and then you can use setContentView() as you wish. Examples of how to do this are easy, but check out the manual and some random example
Community
  • 1
  • 1
Nanne
  • 64,065
  • 16
  • 119
  • 163
0

As Heinrisch said you need to call setContentView before using findViewById(). Why dont you add your custom Relative Layout to your layout xml? Usually you dont need to inflate Views by yourself.

<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>
Renard
  • 6,909
  • 2
  • 27
  • 23