dimanche 22 février 2015

Image slide ViewPager

I'm trying to implement in my application the viewpager to swipe from one image to another. I found a lot of codes in internet but every code take the images from drawable folder. I've got a database with the path of the image on the sdcard so I tried this method to put the image in an array but it won't work saying this



02-22 17:17:23.424: E/AndroidRuntime(6954): FATAL EXCEPTION: main
02-22 17:17:23.424: E/AndroidRuntime(6954): Process: info.androidhive.tabsswipe, PID: 6954
02-22 17:17:23.424: E/AndroidRuntime(6954): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.tabsswipe/info.androidhive.tabsswipe.ViewActivity W}: java.lang.NullPointerException: Attempt to get length of null array
02-22 17:17:23.424: E/AndroidRuntime(6954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-22 17:17:23.424: E/AndroidRuntime(6954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-22 17:17:23.424: E/AndroidRuntime(6954): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-22 17:17:23.424: E/AndroidRuntime(6954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)


this is my code :



public class ViewActivityW extends Activity {
RegistrationAdapterW regadapter;
ViewPager viewPager;
RegistrationOpenHelperW openHelper;
public long rowID;
Cursor c;
public TextView name;
private TextView coff;
private TextView desc;
public EditText viewimg1, viewimg2;
TouchImageView img1;
public String[] imageId;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_w);
name = (TextView) findViewById(R.id.et_viewfnamew);
coff = (TextView) findViewById(R.id.et_viewdescw);
desc = (TextView) findViewById(R.id.et_viewlnamew);
Bundle extras = getIntent().getExtras();
rowID = extras.getLong(WeedFragment.ROW_ID);
viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new CustomAdapter(ViewActivityW.this);
viewPager.setAdapter(adapter);

}

@Override
protected void onResume() {
super.onResume();

// Execute LoadNotes() AsyncTask
new LoadNotes().execute(rowID);
}
private class LoadNotes extends AsyncTask<Long, Object, Cursor> {
// Calls DatabaseConnector.java class
RegistrationAdapterW dbConnector = new RegistrationAdapterW(ViewActivityW.this);

@Override
protected Cursor doInBackground(Long... params) {
// Pass the Row ID into GetOneNote function in
// DatabaseConnector.java class
dbConnector.opnToWrite();
return dbConnector.GetOneNote(params[0]);
}

@Override
protected void onPostExecute(Cursor c) {
super.onPostExecute(c);

c.moveToFirst();

// Set the Text in TextView
name.setText(c.getString(1));
coff.setText(c.getString(2));
desc.setText(c.getString(3));
imageId = new String[] {c.getString(5), c.getString(6)};

c.close();
dbConnector.Close();
}
}
public class CustomAdapter extends PagerAdapter{

Context context;
ViewActivityW view;


public CustomAdapter(Context context){
this.context = context;

}


@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub

LayoutInflater inflater = ((Activity)context).getLayoutInflater();

View viewItem = inflater.inflate(R.layout.image_item, container, false);
ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(view.imageId[position]));
//TextView textView1 = (TextView) viewItem.findViewById(R.id.textView1);
//textView1.setText("hi");
((ViewPager)container).addView(viewItem);

return viewItem;
}

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

@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub

return view == ((View)object);
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}

}


}


What I made wrong? Can someone help me?


Aucun commentaire:

Enregistrer un commentaire