在我的Android应用程序中,我需要使用Dialog Fragment来显示一些可点击的imageViews并选择一种颜色,主要活动将使用该颜色,但我的代码存在问题。 我创建了一个自定义ArrayAdapter,它将ColorCircle类作为参考。这是ArrayAdapter
public class ColorCircleAdapter extends ArrayAdapter {
Context context;
int resource;
ColorCircle [] objects;
public ColorCircleAdapter(Context context, int resource, ColorCircle[] objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.objects = objects;
}
@Override
public View getView(int position,View convertView, ViewGroup parent){
View row = convertView;
ColorCircleHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ColorCircleHolder();
holder.circleIcon = (ImageView)row.findViewById(R.id.icon_color);
row.setTag(holder);
}
else
{
holder = (ColorCircleHolder)row.getTag();
}
ColorCircle singleCircle = objects[position];
holder.circleIcon.setImageResource(singleCircle.icon);
return row;
}
static class ColorCircleHolder{
ImageView circleIcon;
}
这是我在onCreateDialog
中使用的代码lertDialog.Builder builder = new AlertDialog.Builder(getActivity());
ColorCircle [] colorCircles = new ColorCircle[] {
new ColorCircle(R.drawable.blu)
};
ColorCircleAdapter adapter = new ColorCircleAdapter(getActivity(), R.layout.color_view, colorCircles);
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_color_dialog, null);
ListView listView = (ListView)view.findViewById(R.id.color_list);
listView.setAdapter(adapter);
builder.setTitle(R.string.pick_color)
.setAdapter(adapter, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
mListener.onListElementSelected(ColorDialogFragment.this,which);
}
});
return builder.create();
但是它不起作用,当我点击应该打开对话框的图标时,应用程序崩溃了。我在工作流程中缺少什么?是否有可能在我调试时它永远不会进入重写的getView? 这是我得到的错误
thread exiting with uncaught exception (group=0x418b4d40)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
at android.app.Dialog.show(Dialog.java:262)
at android.app.DialogFragment.onStart(DialogFragment.java:493)
at android.app.Fragment.performStart(Fragment.java:1724)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
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)
答案 0 :(得分:0)
适配器代码没有问题。 requestfeature()
发生例外情况。它应该以这种方式调用:
@Override
public void onCreate(Bundle savedInstanceState) {
//HERE
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
}