findViewByIdのスレッドを多数読み込んでカスタムクラスのnullを返しましたが、エラーを見つけることができませんでした。私はFragment(LinearLayout)を膨らませる主なアクティビティを持っています。ハンドラを使用して、このLinearLayoutに私のカスタムImageViewのいくつかを挿入したいと思います。ハンドラは、基本的なImageViewを基本的に作成し、主なアクティビティで読み込むことができるArrayListに配置します。私のカスタムImageViewのでfindViewByIdは、ハンドラクラスを使用してカスタムImageViewのnullを返します
私は、次のコンストラクタを持っている:私はかどうかをテストするには、以下のか、私の主な活動で
customViews = new ArrayList<CustomView>();
for (int k = 0; k < numberOfCustomViews; k++){
CustomView w = new CustomView(ctx,
scanner.nextInt(), //ID
scanner.next(), //Color
scanner.next(), //Size
scanner.nextInt(), //Relative ID
scanner.nextInt(), //Relative angle
scanner.nextFloat(), //Absolute x (percent)
scanner.nextFloat(), //Absolute y (percent)
new int[] {scanner.nextInt(), //Dependence numbers
scanner.nextInt()});
customViews.add(w);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
w.getSizeAsResource(), w.getSizeAsResource());
w.setLayoutParams(lp);
Log.d(TAG, "CustomView added!");
}
:、
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, int ID, String c, String s, int relID, int relAngle,
Float absX, Float absY, int [] inNumbers) {
super(context);
this.setId(ID);
color = c;
size = s;
x_pos = absX;
y_pos = absY;
dependence = inNumbers;
rel_id = relID;
rel_angle = relAngle;
}
その後、私のハンドラクラスで私は次の操作を行います最初のCustomViewは利用可能です:
customViewTest = (CustomView) (findViewById(customViewHandler.customViews.get(1).getId()));
if (customViewTest == null)
Toast.makeText(this, "customViewTest == null", Toast.LENGTH_LONG).show();
これは常にトーストを示しています。私はここで何が欠けていますか?私はいくつかの膨張か何か必要がありますか?プログラムでレイアウトにカスタムビューを追加することは可能です。
私のコードでカスタムImageViewsを追加しない理由は、nullPointerExceptionsを生成するためです。主なアクティビティでnullかどうかをチェックするのではなく、そこで利用可能なViewGroup(findViewByIdを使用)にそれらを追加したいと思います。 私のフラグメントのViewGroupにデータを入れようとすると、孤立したカスタムImageViewsが孤立していると言いますが、私の孤児は必要な時点でnullであるため、これを行うことはできません。 –