我想要扩展一个Gallery和Text,所以我创建了一个gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout00"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_gravity="left|center_vertical" android:textSize="20dip"
android:layout_marginLeft="10dip" />
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
我想展示多个图库,所以我动态添加图库。 当我给它充气时,我得到空指针异常。
public class PayPerViewActivity extends Activity implements OnItemClickListener{
GridView gridview = null;
ImageAdapter imageAdapter = null;
private Gallery gallery[] ;
DataProvider dataProvider ;
LazyAdapter adapter[];
String[] values = null;
View view[];
static class ViewHolder extends LinearLayout{
public ViewHolder(Context context) {
super(context);
}
public Gallery g;
public TextView text;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleris);
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int count = 1;
gallery = new Gallery[Config.NO_OF_GALLERY];
adapter = new LazyAdapter[Config.NO_OF_GALLERY];
LinearLayout innerLayout = (LinearLayout) findViewById(R.id.LinearLayout02);
for(int i = 0 ; i < Config.NO_OF_GALLERY ; i++){
values = new String[Config.IMAGES_PER_GALLERY];
for(int j = 0 ; j<Config.IMAGES_PER_GALLERY ;j++){
values[j]= Config.MOVIE_URL+count+Config.IMAGE_EXT;
count++;
}
ViewHolder vi = new ViewHolder(this);
view[i] = inflater.inflate(R.layout.gallery, null);// Null pointer exception on this line
gallery[i] = (Gallery) view[i].findViewById(R.id.gallery);
adapter[i] = new LazyAdapter(this,dataProvider.getInstance().getGallery().get(""+i).getData());
gallery[i].setAdapter(adapter[i]);
gallery[i].setOnItemClickListener(this);
vi.g = gallery[i];
TextView textView = (TextView)findViewById(R.id.text);
textView.setText("kid");
vi.text = textView;
view[i].setTag(vi);
innerLayout.addView(view[i]);
}
} @Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
String tag = (String) v.getTag();
Intent i = new Intent(this, DetailedViewActivity.class);
i.putExtra("url", tag);
startActivity(i);
}
我无法调试为什么它会给出空指针异常。请帮帮我。感谢。
答案 0 :(得分:1)
您的view
变量为空。在将view[i]
设置为某个内容之前,您需要对其进行初始化,例如
view = new View[Config.NO_OF_GALLERY];
为gallery
和adapter
数组执行此操作。
或者使用集合(特别是List
)。