有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

对话框中的java Android gallery视图导致nullpointerexception

所以我在其他地方使用了下面的代码,它工作得很好,但现在我想在警报对话框中使用它。问题是,无论何时设置适配器,都会导致nullpointerexception。代码(不包括alertdialog)在这里的开发教程中几乎没有问题: http://developer.安卓.com/resources/tutorials/views/hello-gallery.html

如果我把这句话注释掉: 陈列室setAdapter(新的ImageAdapter(本)); 对话框可以正常打开,但设置适配器的那一刻会导致错误。有什么想法吗

以下是我的alertdialog的代码:

    private void statusbarCustom() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.custom_icon, null);
        final EditText cTitle = (EditText)view.findViewById(R.id.search_term);  
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        builder.setView(view);        

        builder.setPositiveButton("Continue", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {                    
             }
        });

        builder.setNegativeButton("Cancel", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.setTitle("Statusbar");
        alertDialog.show();
        }

以下是imageadapter代码:

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.attach,
                R.drawable.bell,
                R.drawable.book_addresses,
                R.drawable.book,
                R.drawable.cake,
                R.drawable.calculator,
                R.drawable.calendar,
                R.drawable.camera,
                R.drawable.car,
                R.drawable.cart,
                R.drawable.chart_curve,
                R.drawable.chart_pie_edit,
                R.drawable.clock_,
                R.drawable.computer,
                R.drawable.controller,
                R.drawable.cup,
                R.drawable.date,
                R.drawable.emotion_evilgrin,
                R.drawable.emotion_grin,
                R.drawable.emotion_happy,
                R.drawable.emotion_smile,
                R.drawable.emotion_suprised,
                R.drawable.emotion_tongue,
                R.drawable.emotion_unhappy,
                R.drawable.emotion_waii,
                R.drawable.emotion_wink,
                R.drawable.exclamation,
                R.drawable.film,
                R.drawable.folder,
                R.drawable.group,
                R.drawable.heart,
                R.drawable.house,
                R.drawable.key,
                R.drawable.lightbulb,
                R.drawable.lightning,
                R.drawable.lock,
                R.drawable.lorry,
                R.drawable.map,
                R.drawable.money_euro,
                R.drawable.money_pound,
                R.drawable.money_yen,
                R.drawable.money,
                R.drawable.shop,
                R.drawable.compass,
                R.drawable.sofa,
                R.drawable.gift,
                R.drawable.smartphone,
                R.drawable.accept,
                R.drawable.add,
                R.drawable.sound_none,
                R.drawable.newspaper,
                R.drawable.painbrush,
                R.drawable.rainbow,
                R.drawable.report,
                R.drawable.ruby,
                R.drawable.shield,
                R.drawable.sport_8ball,
                R.drawable.sport_basketball,
                R.drawable.sport_football,
                R.drawable.sport_raquet,
                R.drawable.sport_shuttlecock,
                R.drawable.sport_soccer,
                R.drawable.sport_tennis,
                R.drawable.star,
                R.drawable.stop,
                R.drawable.table_icon,
                R.drawable.telephone,
                R.drawable.television, 
                R.drawable.facebook
        };

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray attr = mContext.obtainStyledAttributes(R.styleable.GalleryTheme);
            mGalleryItemBackground = attr.getResourceId(
                    R.styleable.GalleryTheme_安卓_galleryItemBackground, 0);
            attr.recycle();
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);

            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(mGalleryItemBackground);

            return imageView;
        }
    }

下面是logcat输出:

02-21 10:40:29.317: E/AndroidRuntime(3347): FATAL EXCEPTION: main
02-21 10:40:29.317: E/AndroidRuntime(3347): java.lang.NullPointerException
02-21 10:40:29.317: E/AndroidRuntime(3347):     at com.flufflydelusions.app.enotesclassic.NoteEdit.statusbarCustom(NoteEdit.java:1954)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at com.flufflydelusions.app.enotesclassic.NoteEdit.access$67(NoteEdit.java:1949)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at com.flufflydelusions.app.enotesclassic.NoteEdit$25.onClick(NoteEdit.java:1835)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at com.安卓.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:935)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.widget.AdapterView.performItemClick(AdapterView.java:284)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.widget.ListView.performItemClick(ListView.java:3746)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.widget.AbsListView$PerformClick.run(AbsListView.java:1981)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.os.Handler.handleCallback(Handler.java:587)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.os.Handler.dispatchMessage(Handler.java:92)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.os.Looper.loop(Looper.java:130)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at 安卓.app.ActivityThread.main(ActivityThread.java:3691)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at java.lang.reflect.Method.invoke(Method.java:507)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:665)
02-21 10:40:29.317: E/AndroidRuntime(3347):     at dalvik.system.NativeStart.main(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    中缺少“视图” 看法画廊的findViewById