2014-04-27 20 views
0

我在Android中創建圖像庫應用程序,但在運行時我給了我一個空點異常。請幫我改正它。這裏是我的代碼圖像庫上的空點異常

我用的畫廊和ImageSwitcher爲XML設計

我在這裏當用戶點擊圖庫圖像動畫前進ImageSwitcher

public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{ 

private ImageSwitcher mswitcher; 
private Integer[] mThimId={R.drawable.fba,R.drawable.fbb,R.drawable.fbc,R.drawable.fbd,R.drawable.fbe,R.drawable.fbf,R.drawable.fbg,R.drawable.fbh,R.drawable.fbi,R.drawable.fbj}; 
private Integer[] mImageId={R.drawable.fba,R.drawable.fbb,R.drawable.fbc,R.drawable.fbd,R.drawable.fbe,R.drawable.fbf,R.drawable.fbg,R.drawable.fbh,R.drawable.fbi,R.drawable.fbj}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mswitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher1); 
    mswitcher.setFactory(this); 
    mswitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 
    mswitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 
    Gallery g=(Gallery) findViewById(R.id.gallery1); 
    g.setAdapter(new ImageAdapter(this)); 
    g.setOnItemSelectedListener(this); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // TODO Auto-generated method stub 
    mswitcher.setImageResource(mImageId[arg2]); 

} 

public class ImageAdapter extends BaseAdapter { 
    private Context mcontext; 
    public ImageAdapter(Context c) { 
     // TODO Auto-generated constructor stub 
     mcontext=c; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return mThimId.length; 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     ImageView i = new ImageView(mcontext); 
     i.setImageResource(mThimId[position]); 
     i.setAdjustViewBounds(true); 
     i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
     return null; 
    } 
} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 
} 

@Override 
public View makeView() { 
    // TODO Auto-generated method stub 
    ImageView i = new ImageView(this); 
    i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 
    return null; 
} 
} 

這裏是我的XML文件中的代碼

<Gallery 
    android:id="@+id/gallery1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="48dp" 
    android:layout_marginTop="36dp" /> 

<ImageSwitcher 
    android:id="@+id/imageSwitcher1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="159dp" 
    android:layout_marginLeft="63dp" > 
</ImageSwitcher> 

回答

0

您應該從getView和makeView方法返回'i'而不是null。