Question

I'm creating an image gallery application in Android but I gives me a null point exception at run-time. Please help me to correct it. Here is my code

I use Gallery and ImageSwitcher for XML design

I here when user click the gallery images move in animation on 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;
}
}

Here is my XML file code

 <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>
Was it helpful?

Solution

You should be returning 'i' instead of null from the getView and makeView methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top