我尝试创建自定义ViewGroup类,但是当我使用方法findViewById()时它返回 null,但是膨胀视图是可以的。
代码是:
public class HorizontalListView extends ViewGroup
{
private int mNumber = 0;
private ImageView mImage;
private LinearLayout mAdapter;
public HorizontalListView(final Context context, final AttributeSet set)
{
super(context, set);
LayoutInflater.from(getContext()).inflate(R.layout.layout_horizontal_list_view, this, false);
mAdapter = (LinearLayout) getChildAt(0);
}
/**Adds ImageView to LinearLayout (Adapter)
* g
* @param image
*/
public void addView(final Bitmap image)
{
mImage = (ImageView) LayoutInflater.from(getContext())
.inflate(R.layout.create_added_photo, null);
mImage.setImageBitmap(image);
mImage.setTag(mNumber);
mNumber++;
mAdapter.addView(mImage);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub
}
}
这里mAdapter.addView(mImage); 我有一个 NullPointerException
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:id="@+id/list_for_new_photos">
</LinearLayout>
</HorizontalScrollView>