Alertdialog gridview错误

时间:2017-01-05 05:45:47

标签: java android nullpointerexception

我需要在警告对话框中显示gridview。但我坚持错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference

我的代码......

listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
            //listview click event handling
           TextView id = (TextView) arg1.findViewById(R.id.textView17);
            final int id_To_Search = Integer.valueOf(id.getText().toString());
            Cursor item=mydb.singlecons(id_To_Search);
            Cursor att=mydb.attrs(id_To_Search);
            Cursor picloc=mydb.singleconspic(id_To_Search);
            att.moveToFirst();
            List<String> list = new ArrayList<>();
          //  Log.d("temp",att.getColumnName(1));

            while (!att.isAfterLast())
            {
                int l=att.getColumnCount();
                Log.d("length", String.valueOf(l));
                for(int i=2;i<l;i++){
                Log.d("for","for");
                    if(att.getString(i)!=null){
                        String b= att.getColumnName(i)+" "+att.getString(i);
                        list.add(b);
                        Log.d("att",b);
                    }
                }
                Log.d("while","while");
                att.moveToNext();
            }
            att.close();
            Log.d("list", String.valueOf(list));
            picloc.moveToFirst();

            FilePathStrings = new String[picloc.getCount()];
            int i=0;
            while (!picloc.isAfterLast()){
                Log.d("picloc",picloc.getString(2));
                FilePathStrings[i]=picloc.getString(2);
                i++;
                picloc.moveToNext();
            }

            item.moveToFirst();

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
            dialogBuilder.setView(dialogView);

            TextView con=(TextView)dialogView.findViewById(R.id.textView29);
            con.setText("Consultation on "+item.getString(4));
            TextView des=(TextView)dialogView.findViewById(R.id.textView28);
            des.setText(item.getString(2));
            TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
            pre.setText(item.getString(3));
            TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
            fee.setText(item.getString(5));
            adapter1 = new GridViewAdapter(FilePathStrings,getApplicationContext());
            grid.setAdapter(adapter1);
            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();

Gridviewadapter.java

class GridViewAdapter extends BaseAdapter {

private String[] filepath;
private static LayoutInflater inflater = null;
GridViewAdapter(String[] fpath,Context context) {
    filepath = fpath;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
public int getCount() {
    return filepath.length;

}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
        ImageView image = (ImageView) vi.findViewById(R.id.image);
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
        image.setImageBitmap(bmp);
        return vi;
}

}

Alertlabeleditor.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="20dp">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView29"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="24sp"
            android:layout_marginBottom="10dp"
            android:textStyle="normal|bold" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="Description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView27"
                    android:layout_weight="2" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView28"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="Prescription"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView30"
                    android:layout_weight="2" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView31"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="fee"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView33"
                    android:layout_weight="2" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView32"
                    android:layout_weight="1" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />

</LinearLayout>

我该如何解决这个问题。我的代码中出现错误在哪里?我知道由于从没有值的地方访问值而导致空指针错误,但我无法弄清楚这是在哪里发生的,我该如何解决它。我该如何解决这个问题。?

1 个答案:

答案 0 :(得分:0)

您的GridView引用为null

确保您已初始化GridView参考

示例

grid = (GridView) dialogView.findViewById(R.id.gridview);

用此

替换你的onclickListner
listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
        //listview click event handling
       TextView id = (TextView) arg1.findViewById(R.id.textView17);
        final int id_To_Search = Integer.valueOf(id.getText().toString());
        Cursor item=mydb.singlecons(id_To_Search);
        Cursor att=mydb.attrs(id_To_Search);
        Cursor picloc=mydb.singleconspic(id_To_Search);
        att.moveToFirst();
        List<String> list = new ArrayList<>();
      //  Log.d("temp",att.getColumnName(1));

        while (!att.isAfterLast())
        {
            int l=att.getColumnCount();
            Log.d("length", String.valueOf(l));
            for(int i=2;i<l;i++){
            Log.d("for","for");
                if(att.getString(i)!=null){
                    String b= att.getColumnName(i)+" "+att.getString(i);
                    list.add(b);
                    Log.d("att",b);
                }
            }
            Log.d("while","while");
            att.moveToNext();
        }
        att.close();
        Log.d("list", String.valueOf(list));
        picloc.moveToFirst();

        FilePathStrings = new String[picloc.getCount()];
        int i=0;
        while (!picloc.isAfterLast()){
            Log.d("picloc",picloc.getString(2));
            FilePathStrings[i]=picloc.getString(2);
            i++;
            picloc.moveToNext();
        }

        item.moveToFirst();

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
        dialogBuilder.setView(dialogView);

        TextView con=(TextView)dialogView.findViewById(R.id.textView29);
        con.setText("Consultation on "+item.getString(4));
        TextView des=(TextView)dialogView.findViewById(R.id.textView28);
        des.setText(item.getString(2));
        TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
        pre.setText(item.getString(3));
        TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
        fee.setText(item.getString(5));
        adapter1 = new GridViewAdapter(FilePathStrings,getApplicationContext());
        grid = (GridView) dialogView.findViewById(R.id.gridview);
        grid.setAdapter(adapter1);
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();