私のアクティビティでは、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);
}
findViewByIdを呼び出す前に、コンテンツビューをレイアウトに設定する必要があります。アクティビティコードを投稿できますか? – Heinrisch
がメインポストの編集として追加されました – krackmoe
しかし、どうすればいいですか? 私はカスタム相対レイアウトであるカスタムDrawViewを持っているので、このレイアウトでGridViewを追加したいのですが... – krackmoe