列表视图中有2种不同的布局

时间:2014-10-29 06:53:15

标签: android

我有什么

我有一个导航抽屉,其中listview是自定义的。第一行有imageview,其他行有textview。我的适配器代码是:

public class drawer_list_adapter extends BaseAdapter {

Context context;
String[] nv_items;
int[] pro_pic;
LayoutInflater layoutInflater;

public drawer_list_adapter(Context context, String[] nv_items) {
    this.context = context;
    this.nv_items = nv_items;
    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);
        }
         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;
    }
}

这里的一切都很好。

现在我要更改此列表视图中的图像。用户可以从画廊中选择图像。我的代码如下:

private class DrawerItemClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        if (position == 0){
            Intent pickFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_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_selected.setImageBitmap(BitmapFactory.decodeFile(imagePath));
            cursor.close();
        }
    }

我点击第一行,图库打开但是当我选择图像时,我收到以下错误:

Process: com.hirak.assistere_doit, PID: 14010
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hirak.assistere_doit/com.hirak.assistere_doit.do_it_main_screen}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        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.onCreate(do_it_main_screen.java:54)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        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)

我的onCreate()方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_do_it_main_screen);

    addTask = (Button) findViewById(R.id.addTask);
    addNote = (Button) findViewById(R.id.addNote);
    addNote.setOnClickListener(this);
    addTask.setOnClickListener(this);

    final ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    drawerLayout = (DrawerLayout) findViewById(R.id.NvDrawer);
    drawerList = (ListView) findViewById(R.id.drawer_list);
    drawer_list_adapter drawerListAdapter = new drawer_list_adapter(this, nv_items);
    drawerList.setAdapter(drawerListAdapter);
    drawerList.setOnItemClickListener(new DrawerItemClickListener());



    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);

    drawerLayout.setDrawerListener(drawerToggle);
}

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

您应该在onCreate()方法中初始化ImageView pro_pic_selected。即您需要将此行pro_pic_selected = (ImageView) findViewById(R.id.imageView);放在onCreate()中。如果要在类的任何位置使用该View,可以在类do_it_main_screen中的类范围内声明它。此外,请确保布局R.layout.activity_do_it_main_screen中存在带有ID imageView的ImageView,否则findViewById将返回null。