对话框显示错误

时间:2017-08-03 22:35:27

标签: android android-recyclerview android-alertdialog

我在使用onView的cardView上显示此对话框时遇到问题。昨天工作但今天它失败了。

我解决了问题,因为我打电话给我的适配器 MyAdapter(getActivity()。getAplicationContext,getDocumentList()); MyAdapter(getActivity(),getDocumentList());

这是alerDialog的代码。

    private void createDialogServices(final int position) {
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(context);
    builderSingle.setIcon(R.drawable.ic_work_black);
    builderSingle.setTitle("¿Qué oficio desempeñas?");

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.select_dialog_singlechoice);

    GetOficiosFromServer.getAllWorks(context, new GetOficiosFromServer.VolleyCallBack() {
        @Override
        public void onSuccess(ArrayList<String> oficios) {
            for (String profesion : oficios) {
                arrayAdapter.add(profesion);
                //Toast.makeText(context, profesion, LENGTH_LONG).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "Error al recuperar información", LENGTH_LONG).show();
        }
    });

    builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String strName = arrayAdapter.getItem(which);
            //onSelectOficio(position, strName);
        }
    });
    builderSingle.show();
}

高于我的RecyclerView适配器的代码

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_FOOTER = 2;
private HeaderViewHolder headerHolder;


private ArrayList<Doc> documentos;
private Context context;

public MyAdapter(Context context, ArrayList<Doc> generics) {
    this.context = context;
    this.documentos = generics;
}



@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
    if(viewType == TYPE_HEADER) {
        View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_header, parent, false);
        return new HeaderViewHolder (v);
    } else if(viewType == TYPE_FOOTER) {
        View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_foter, parent, false);
        return new FooterViewHolder (v);
    } else if(viewType == TYPE_ITEM) {
        View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_item, parent, false);
        return new GenericViewHolder (v);
    }
    return null;
}

private Doc getItem (int position) {
    return documentos.get (position);
}


@Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, final int position) {
    if(holder instanceof HeaderViewHolder) {
        headerHolder = (HeaderViewHolder) holder;
        headerHolder.card_view_header.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                Toast.makeText (context, "Clicked Header " + position, Toast.LENGTH_SHORT).show ();
                createDialogServices(position);
            }
        });
    } else if(holder instanceof FooterViewHolder) {
        FooterViewHolder footerHolder = (FooterViewHolder) holder;
        //footerHolder.txtTitleFooter.setText ("Footer");
        footerHolder.send_Button.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                Toast.makeText (context, "Clicked Footer", Toast.LENGTH_SHORT).show ();
            }
        });
    } else if(holder instanceof GenericViewHolder) {
        Doc currentItem = getItem (position - 1);
        final Integer num = currentItem.getPosition();
        GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
        genericViewHolder.txtTitle.setText(currentItem.getTittle());
        genericViewHolder.txtDescription.setText(currentItem.getPosition().toString());
        genericViewHolder.txtTitle.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                Toast.makeText (context, "Clicked item" + (num), Toast.LENGTH_SHORT).show ();
            }
        });
    }
}
//    need to override this method
@Override
public int getItemViewType (int position) {
    if(isPositionHeader (position)) {
        return TYPE_HEADER;
    } else if(isPositionFooter (position)) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private boolean isPositionHeader (int position) {
    return position == 0;
}

private boolean isPositionFooter (int position) {
    return position == documentos.size () + 1;
}

@Override
public int getItemCount () {
    return documentos.size () + 2;
}

private class FooterViewHolder extends RecyclerView.ViewHolder {
    Button send_Button;
    FooterViewHolder(View itemView) {
        super (itemView);
        this.send_Button = (Button) itemView.findViewById (R.id.btn_send_docs);
    }
}



private class HeaderViewHolder extends RecyclerView.ViewHolder {
    CardView card_view_header;
    TextView card_txt_title;
    TextView card_txt_description;
    HeaderViewHolder(View itemView) {
        super (itemView);
        this.card_view_header = (CardView) itemView.findViewById (R.id.card_view_header);
        this.card_txt_title = (TextView) itemView.findViewById(R.id.card_header_textView_tittle);
        this.card_txt_description = (TextView) itemView.findViewById(R.id.card_header_textView_description);
    }
}

private class GenericViewHolder extends RecyclerView.ViewHolder {
    TextView txtTitle;
    TextView txtDescription;
    GenericViewHolder(View itemView) {
        super(itemView);
        this.txtTitle = (TextView) itemView.findViewById(R.id.card_item_textView_tittle);
        this.txtDescription = (TextView) itemView.findViewById(R.id.card_item_textView_description);
    }
}

此代码用于工作,但我将页脚和标题添加到RecyclerView,它确实不是问题,因为在此之后我记得它有效,但我正在搞乱一些类型的排球。我认为是一种背景。我也在Activity上测试相同的方法并且工作正常。

我得到的错误是:

 FATAL EXCEPTION: main
  Process: com.bim.tech.construbimapp, PID: 4504
  android.content.res.Resources$NotFoundException: Resource ID #0x0
      at android.content.res.Resources.getValue(Resources.java:1457)
      at android.content.res.Resources.loadXmlResourceParser(Resources.java:2904)
      at android.content.res.Resources.getLayout(Resources.java:1237)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:966)
      at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:942)
      at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:930)
      at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:954)
      at com.bim.tech.construbimapp.helper.MyAdapter.createDialogServices(MyAdapter.java:208)
      at com.bim.tech.construbimapp.helper.MyAdapter.access$100(MyAdapter.java:29)
      at com.bim.tech.construbimapp.helper.MyAdapter$1.onClick(MyAdapter.java:75)
      at android.view.View.performClick(View.java:4848)
      at android.view.View$PerformClick.run(View.java:20262)
      at android.os.Handler.handleCallback(Handler.java:815)
      at android.os.Handler.dispatchMessage(Handler.java:104)
      at android.os.Looper.loop(Looper.java:194)
      at android.app.ActivityThread.main(ActivityThread.java:5637)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

1 个答案:

答案 0 :(得分:0)

我很确定这种情况正在发生,因为Context传入AlertDialog.Builder(context)的{​​{1}}是Application Context而不是Activity }}。

您在android.content.res.Resources$NotFoundException 208的{​​{1}}上投掷MyAdapter

at com.bim.tech.construbimapp.helper.MyAdapter.createDialogServices(MyAdapter.java:208)

显然,AlertDialogRecycleListView膨胀以显示Adapter中的数据时遇到一些问题。

at android.content.res.Resources.getValue(Resources.java:1457)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2904)
at android.content.res.Resources.getLayout(Resources.java:1237)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:966)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:942)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:930)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:954)

如果你看一下AlertController.AlertParams.createListView on line 966,你会发现它只是试图给RecyclerView充气。

final RecycleListView listView =
                    (RecycleListView) mInflater.inflate(dialog.mListLayout, null);

无法找到的资源是dialog.mListLayout。因此,如果我们看一下mListLayout的初始化方式,我们会在AlertController

中看到它being obtained from a TypedArray
    final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog,
            R.attr.alertDialogStyle, 0);
    ...
    mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);
    ...
    a.recycle();

因此,如果您通过ResourceNotFoundException Application传递Context是有意义的,因为属性android.support.v7.appcompat.R.styleable.AlertDialog不是Application。可以从Resource ID #0x0 Theme获取。这就是您看到0的原因,因为return publications.stream() .collect(Collectors.toMap( Publication::getIdPaper, publication -> publication.getPublicationKeyword() .stream() .collect(Collectors.toMap( PublicationKeyword::getKeyword, PublicationKeyword::getConceptFrequency)))); 是返回的默认值。

编辑我在编辑您的OP时输入了这个内容,但我只是为了解释堆栈跟踪而离开它。