2011-08-12 56 views
0

我想膨脹圖庫和文字,所以我創建了一個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); 
} 

我不能調試爲什麼它給予空指針exception.Please幫助我。謝謝。

回答

1

您的view變量爲空。你需要爲你的galleryadapter陣列做設置view[i]的東西之前,對其進行初始化,例如

view = new View[Config.NO_OF_GALLERY]; 

或使用此收藏夾(特別是List)。

+0

我剛剛得到錯誤。你是對的。不管怎麼說,多謝拉。 – Jazzs