What I have and What I want
I have an activity with navigation drawer. In this navigation drawer, I have used ListView with BaseAdapter class. I have 2 viewTypes, the 1st row that have an Imageview. and all other rows with simple text.
Here is my Adapter Class :
public class drawer_list_adapter extends BaseAdapter {
Context context;
String[] nv_items;
int pic;
LayoutInflater layoutInflater;
public drawer_list_adapter(Context context, String[] nv_items, int pic) {
this.context = context;
this.nv_items = nv_items;
this.pic = pic;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return 4;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 4;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder dLI;
if (convertView == null) {
if (getItemViewType(position) == 0){
convertView = layoutInflater.inflate(R.layout.drawer_pro_pic, parent, false);
dLI = new ViewHolder();
convertView.setTag(dLI);
dLI.avatar = (ImageView) convertView.findViewById(R.id.imageView);
dLI.avatar.setImageResource(pic);
}
else {
convertView = layoutInflater.inflate(R.layout.drawer_list_item, parent, false);
dLI = new ViewHolder();
convertView.setTag(dLI);
dLI.items = (TextView) convertView.findViewById(R.id.drawer_list_text);
dLI.items.setText(nv_items[position]);
}
}
else {
dLI = (ViewHolder) convertView.getTag();
}
return convertView;
}
public class ViewHolder {
TextView items;
ImageView avatar;
}
}
Now, in my main activity, i have set adapter and all that stuff. I have also defined arraystring for the 3 rows that have textview.
Here is my onCreate() method (only that part which is concerned with my issue):
drawerLayout = (DrawerLayout) findViewById(R.id.NvDrawer);
drawerList = (ListView) findViewById(R.id.drawer_list);
drawer_list_adapter drawerListAdapter = new drawer_list_adapter(this, nv_items, pic);
drawerList.setAdapter(drawerListAdapter);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
Now about the imageview (the first row), I want user to choose a pic from the gallery. i used startActivityForResult which is called when item is selected in the listview (position == 0 in my case). I can open the Gallery, but when i select the pic, the app crashes.
Below is my code for startActivitForResult and onActivityResult :
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (position == 0){
Intent pickFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickFromGallery, OpenGallery);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OpenGallery && resultCode == RESULT_OK){
Uri pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
pro_pic.setImageBitmap(BitmapFactory.decodeFile(imagePath));
cursor.close();
}
}
And here is the error code :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1019 flg=0x1 }} to activity {com.hirak.assistere_doit/com.hirak.assistere_doit.do_it_main_screen}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.hirak.assistere_doit.do_it_main_screen.onActivityResult(do_it_main_screen.java:116)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
0 comments:
Post a Comment