0

I am getting the following exception

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)

in the following piece of code:

@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;
}

After going through post by person who experienced similar problem, i used handler but that trick did not work for me.

Here is the listener class

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

    }

}

1 Answer 1

2

I think your context is wrong: ContactManagerApplication.context. You need an Activity Context, not an Application context. It seems like you already have a "good" context available when you get the LayoutInflater a few lines before: context.getLayoutInflater().

3
  • 1
    I agree. A tip on efficiency for guptakgaurav, you only need one LayoutInflater. You should make inflator a field variable and initialize it once in the constructor.
    – Sam
    Jan 1, 2013 at 17:35
  • Buddy, you hit the bulls eye... thank you. but one more question, why application context was not working here, what i am concluding now is that application context and activity context are different entities and previously i thought an app consist of app context plus all living activity contexts also. was i wrong previously
    – user1707035
    Jan 1, 2013 at 17:35
  • 1
    Yep, they're different. Most of the time you want to use the Activity context . Here's another question that explains the difference.
    – dmon
    Jan 1, 2013 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.