1

我试图让我的 alertDialog 在 arrayAdapter 中工作。这就是我的功能的样子。

   @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final Noodnummers currentNoodnummer = noodnummers.get(position);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(layoutResourceId, parent, false);

        TextView txt_noodnummer_name = (TextView) rowView.findViewById(R.id.txt_noodnummer_name);
        txt_noodnummer_name.setText(currentNoodnummer.naam);

        TextView txt_noodnummer_telefoon = (TextView) rowView.findViewById(R.id.txt_noodnummer_telefoon);
        txt_noodnummer_telefoon.setText(Html.fromHtml(currentNoodnummer.telefoonNummer));
        txt_noodnummer_telefoon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                //To change body of implemented methods use File | Settings | File Templates.
                try {

                    new AlertDialog.Builder(this)
                            .setTitle("")
                            .setMessage("Bent u zeker dat u" + currentNoodnummer.telefoonNummer + "wilt bellen?")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // continue with delete
                                    String uri = currentNoodnummer.telefoonNummer;
                                    Intent intent = new Intent(Intent.ACTION_CALL);
                                    intent.setData(Uri.parse(uri));
                                    v.getContext().startActivity(intent);
                                }
                            })
                            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // do nothing
                                }

                            })
                            .show();


                } catch (ActivityNotFoundException activityException) {
                    Log.e("genkonstage", "Call failed", activityException);
                }

            }
        });



        return rowView;
    }

第一个问题是,这 new AlertDialog.Builder(this)给了我这个词的错误this。然后我发现我可能应该使用上下文。但是当我说new AlertDialog.Builder(context)我没有收到错误但是当我点击应用程序崩溃时。

有人可以帮我弄这个吗?

编辑

06-18 10:56:20.689  12980-12980/be.appmax.genkOnStage          E/AndroidRuntime: FATAL EXCEPTION: main
        android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:281)
        at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
        at be.appmax.genkOnStage.adapters.NoodnummersArrayAdapter$1.onClick(NoodnummersArrayAdapter.java:57)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

在里面创建arrayAdapterNoodnummersActivity

ListView listView = (ListView) findViewById(R.id.listViewNoodnummers);
//addList(noodnummersArrayList, listView);
NoodnummersArrayAdapter adapter = new NoodnummersArrayAdapter(getBaseContext(), R.layout.noodnummers_list, noodnummersArrayList);

// fill the listview with adapter.
listView.setAdapter(adapter);

setListViewHeightBasedOnChildren(listView);
4

2 回答 2

3

“从不”使用 getBaseContext()!来自 Google 工程师

尝试NoodnummersActivity.this

NoodnummersArrayAdapter adapter = new NoodnummersArrayAdapter(NoodnummersActivity.this, R.layout.noodnummers_list, noodnummersArrayList);

然后尝试

new AlertDialog.Builder(context)

顺便说一句,我建议在您的活动开始时使用上下文定义一个私有字段。我在这里的某个地方找到了这个建议,我觉得它很棒:

public class MyActivity extends Activity {
    private Context context = MyActivity.this;
}

这样您就可以毫无顾虑地使用“上下文”。

于 2013-06-18T08:45:15.630 回答
0

将此方法用于上下文:

private Context getDialogContext() {
        Context context;
        if (getParent() != null)
            context = getParent();
        else
            context = this;
        return context;
    }

试试这个它会工作。

于 2013-06-18T09:15:32.377 回答