Java 对话框中的Android gallery视图导致nullpointerexception
所以我在其他地方使用了下面的代码,它工作得很好,但现在我想在警报对话框中使用它。问题是,无论何时设置适配器,都会导致nullpointerexception。代码(减去alertdialog)在这里的开发教程中基本上是正确的: 如果我把这行注释掉: gallery.setAdapter(新的ImageAdapter(本)); 对话框可以正常打开,但设置适配器的那一刻会导致错误。有什么想法吗 以下是我的alertdialog的代码:Java 对话框中的Android gallery视图导致nullpointerexception,java,android,gallery,Java,Android,Gallery,所以我在其他地方使用了下面的代码,它工作得很好,但现在我想在警报对话框中使用它。问题是,无论何时设置适配器,都会导致nullpointerexception。代码(减去alertdialog)在这里的开发教程中基本上是正确的: 如果我把这行注释掉: gallery.setAdapter(新的ImageAdapter(本)); 对话框可以正常打开,但设置适配器的那一刻会导致错误。有什么想法吗 以下是我的alertdialog的代码: private void statusbarCusto
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_android_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.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:935)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.widget.ListView.performItemClick(ListView.java:3746)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1981)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.os.Handler.handleCallback(Handler.java:587)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.os.Looper.loop(Looper.java:130)
02-21 10:40:29.317: E/AndroidRuntime(3347): at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
02-21 10:40:29.317: E/AndroidRuntime(3347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
02-21 10:40:29.317: E/AndroidRuntime(3347): at dalvik.system.NativeStart.main(Native Method)
检查您的
图库是否为空。中缺少“视图”
此行中画廊的view.findViewById:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
画廊必须位于通过setContentView设置的视图中。setContentView也必须位于findViewByID之前。否则,您需要使用布局充气器并获得画廊视图:
View view = activityContext.getLayoutInflater().inflate(R.layout.gallery_xml, null);
Gallery gallery = (Gallery)view.findViewById(R.id.gallery);
发现问题…我是个白痴哈哈。我在:Gallery Gallery=(Gallery)view.findViewById(R.id.Gallery)中缺少“view”;因此,这意味着在当前城市中没有id=R.id.gallery的视图。您可能需要在视图
上调用findViewById
?