Java Alertdialog gridview错误

Java Alertdialog gridview错误,java,android,nullpointerexception,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.O

我需要在警报对话框中显示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();
}

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>


我怎样才能解决这个问题。?我的代码中发生的错误在哪里?我知道由于从没有值的位置访问值而导致的空指针错误,但我无法确定这是在哪里发生的以及如何解决它。如何解决此问题。

您的
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();
listView\u prev.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(适配器视图arg0,最终视图arg1,内部视图arg2,长视图arg3){
//listview单击事件处理
TextView id=(TextView)arg1.findViewById(R.id.textView17);
final int id_To_Search=Integer.valueOf(id.getText().toString());
游标项=mydb.singlecons(id到搜索);
游标att=mydb.attrs(id到搜索);
游标picloc=mydb.singlecommercec(id到搜索);
附件:moveToFirst();
列表=新的ArrayList();
//Log.d(“temp”,att.getColumnName(1));
而(!att.isAfterLast())
{
int l=att.getColumnCount();
Log.d(“长度”,String.valueOf(l));

for(inti=2;在这里,它的
grid
为空
grid.setAdapter(adapter1)
我该如何解决它。你能解释一下吗?肯定有人会帮你的,你只需要发布相关的错误代码并完成logcat,一直这样做扫描你现在更新错误日志并向我们显示alert\u label\u editor xml文件谢谢…它修复了它…但是alertdialog非常慢..有时它会显示
java.lang.OutOfMemoryError:F无法在dalvik.system.VMRuntime.newNonMovableArray(本机方法)上分配23970828字节分配,其中16777168个空闲字节和18MB的内存
如果要在gridview中加载图像,请尝试使用毕加索或Glide加载。也请查看此答案。我已经查看了ans,但它可以使用反灰化,但当我单击列表时,设备会冻结5秒钟并显示警报对话框
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();