Getting null pointer exception when inflate a layout
I want to inflate a Gallery and Text, so I creates a 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>
I want to show multiple gallery , so i add galleries dynamically. When I inflate it , I am getting null pointer exception.
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);
}
I am not able to debug why it giving null pointer exception.Please help me. Thanks.
Your view
variable is null. You need to initialize it before setting view[i]
to something, for example
view = new View[Config.NO_OF_GALLERY];
as you do it for gallery
and adapter
arrays.
Or use collections for this (particularly List
).
精彩评论