2012-04-15 13 views
0

私のアクティビティでは、android gridview常にnull

DrawingViewはカスタム相対レイアウトです。

このRelativeLayoutは、カスタムNavigationBarがDrawViewにここに追加されたカスタムNavigationbarとカスタムGridViewの

で存在している必要があります:私は、GridViewのと似た何かをしたかった

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

..

私はそれを取得しない、私の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); 
     } 

    } 

EDIT:Heinrischあなたは()findViewByIdを使用する前に、setContentViewを呼び出す必要が言ったように

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

findViewByIdを呼び出す前に、コンテンツビューをレイアウトに設定する必要があります。アクティビティコードを投稿できますか? – Heinrisch

+0

がメインポストの編集として追加されました – krackmoe

+0

しかし、どうすればいいですか? 私はカスタム相対レイアウトであるカスタムDrawViewを持っているので、このレイアウトでGridViewを追加したいのですが... – krackmoe

答えて

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のレイアウトが存在する、これはすべてが構築されているrelativelayoutです – krackmoe

+0

どのレイアウトが使用され、何が呼び出されているかを明確に示すために質問を更新できますか? – erbsman

+0

ok i更新それは – krackmoe

0

。 カスタム相対レイアウトをレイアウト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

2

あなたは、2つのオプションあなたがfindViewByIdを使用

  1. を持っています。あなたが探しているビューは、setContentView()を使って "画面上"でなければなりません。それ以外の方法はありません。機能はfindViewByIdです。
  2. このビューをプログラムで設定する必要があり、何らかの理由で設定したいXMLに含まれていないため、setContentView()を使用できない場合は、INFLATEを表示してから、あなたの魔法を実行してくださいあなたが望むようにsetContentView()を使用することができます。これを行う方法の例は簡単ですが、the manualと一部を確認してください。random example
関連する問題