0

我收到以下异常

01-01 22:42:52.184: E/AndroidRuntime(408): FATAL EXCEPTION: main
01-01 22:42:52.184: E/AndroidRuntime(408): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.view.ViewRoot.setView(ViewRoot.java:531)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.app.Dialog.show(Dialog.java:241)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
01-01 22:42:52.184: E/AndroidRuntime(408):  at com.gaurav.contactmanager.controller.MyContactAdapter$2$1.run(MyContactAdapter.java:103)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.os.Handler.handleCallback(Handler.java:587)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.os.Looper.loop(Looper.java:123)
01-01 22:42:52.184: E/AndroidRuntime(408):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-01 22:42:52.184: E/AndroidRuntime(408):  at java.lang.reflect.Method.invokeNative(Native Method)
01-01 22:42:52.184: E/AndroidRuntime(408):  at java.lang.reflect.Method.invoke(Method.java:507)
01-01 22:42:52.184: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-01 22:42:52.184: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-01 22:42:52.184: E/AndroidRuntime(408):  at dalvik.system.NativeStart.main(Native Method)

在以下代码中:

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

    ViewHolder viewHolder;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.layout_list_view, null);
        viewHolder = new ViewHolder();
        viewHolder.contactname = (TextView) convertView
                .findViewById(R.id.contactname);
        viewHolder.contactnumber = (TextView) convertView
                .findViewById(R.id.contactnumber);
        viewHolder.contactbadge = (QuickContactBadge) convertView
                .findViewById(R.id.contactBadge);
        viewHolder.img_logo_fb = (ImageButton) convertView
                .findViewById(R.id.img_logo_fb);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if (contactlist.get(position) == null) {
        Log.d(ContactManagerUtil.TAG_ENTER,
                "In adapter getView, found: contact null");
        return null;
    }
    viewHolder.contactname.setText(contactlist.get(position).getName());
    viewHolder.contactnumber.setText(contactlist.get(position).getNumber());
    viewHolder.img_logo_fb.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Toast.makeText(ContactManagerApplication.context,
                    "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
            // TODO viewTimeLine of this contact
            FBExplorer.viewTimeLine();
        }
    });
    final String[] cs = { "View TimeLine", "Post on Wall", "Send a message" };
    viewHolder.img_logo_fb
            .setOnLongClickListener(new OnLongClickListener() {

                public boolean onLongClick(View v) {
                    // TODO create a dialog box here
                    if (ContactManagerApplication.context == null) {
                        Toast.makeText(ContactManagerApplication.context,
                                "Context is null", Toast.LENGTH_SHORT)
                                .show();
                    }
                    new Handler().postDelayed(new Runnable() {

                        public void run() {
                            Log.d("@gaurav", "In run() of handler");
                            // TODO Auto-generated method stub
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    ContactManagerApplication.context);
                            builder.setTitle("Choose your option");
                            builder.setItems(cs,new OnPressOnLongClickListener());
                            builder.create();
                            builder.show();
                        }
                    }, 1000);

                    return true;
                }// onLongClick ends
            });


    return convertView;
}

在经历过类似问题的人发帖后,我使用了处理程序,但那个技巧对我不起作用。

这是监听器类

package com.gaurav.contactmanager.controller;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class OnPressOnLongClickListener implements OnClickListener {

    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }

}
4

1 回答 1

2

我认为您的上下文是错误的:ContactManagerApplication.context. 您需要一个活动上下文,而不是应用程序上下文。当您在几行之前获得 LayoutInflater 时,您似乎已经有了一个“好的”上下文可用context.getLayoutInflater()

于 2013-01-01T17:26:20.990 回答