App crashed when AlertDialog is about
<p>I have a list view, the contain is populated from a database. And I put a button on each row, when it's clicked it shows an AlertDialog box.</p>

<p>I have no errors on my project, but the app crashes everytime I click the button for the AlertDialog box.</p>

<p>Here's my code:</p>

<pre><code>donate_now.setOnClickListener(new OnClickListener() {
 public void onClick(View view) {
   AlertDialog.Builder alertbox = new AlertDialog.Builder(ProjectsList.this);
   alertbox.setTitle("SMS");
   alertbox.setMessage (Html.fromHtml(("Ihr SMS wird vorbereitet. Bitte 
                        bestätigen Sie mit 'Ok' und senden Sie danach die SMS ab! 
                        &lt;p&gt;&lt;small&gt;Sie helfen dem Projekt mit " +mPriceIndex + ". 
                        Betrag je SMS zzgl. Transport. Der Betrag abzüglich 17 Cent 
                        geht direkt an die Partner-Organisation.")));                                                   
 alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface arg0, int arg1) {
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.putExtra("address", mShortcodeIndex); 
    sendIntent.putExtra("sms_body", mKeywordIndex); 
    sendIntent.setType("vnd.android-dir/mms-sms");
    startActivity(sendIntent);
   }   
  });
  alertbox.show();
</code></pre>

<p>Can anybody help me analyze where do I go wrong?</p>

<p>Here's the debug:</p>

<pre><code>11-14 10:38:37.834: E/AndroidRuntime(509): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44c1e1b0 is not valid; is your activity running?
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewRoot.setView(ViewRoot.java:468)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.app.Dialog.show(Dialog.java:239)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
11-14 10:38:37.834: E/AndroidRuntime(509):  at spendino.de.ProjectsList$MySimpleCursorAdapter$2.onClick(ProjectsList.java:213)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.View.performClick(View.java:2364)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.View.onTouchEvent(View.java:4179)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.View.dispatchTouchEvent(View.java:3709)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.os.Looper.loop(Looper.java:123)
11-14 10:38:37.834: E/AndroidRuntime(509):  at android.app.ActivityThread.main(ActivityThread.java:4363)
11-14 10:38:37.834: E/AndroidRuntime(509):  at java.lang.reflect.Method.invokeNative(Native Method)
11-14 10:38:37.834: E/AndroidRuntime(509):  at java.lang.reflect.Method.invoke(Method.java:521)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-14 10:38:37.834: E/AndroidRuntime(509):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-14 10:38:37.834: E/AndroidRuntime(509):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Here's the full activity code:</p>

<pre><code>public class ProjectsList extends Activity implements OnItemClickListener{
    /** Called when the activity is first created. */

    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.projects_list);

        doSync();
        ListView listView = (ListView) findViewById(R.id.lstText);
        listView.setOnItemClickListener(this);
        listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
                managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
                        Database.Project.NAME), new String[] { BaseColumns._ID,
                        Database.Project.C_PROJECTTITLE,
                        Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE, Database.Project.C_DONATIONAMOUNT, Database.Project.C_KEYWORD, Database.Project.C_PRICE, Database.Project.C_SHORTCODE}, null, null, null),
                new String[] { Database.Project.C_PROJECTTITLE,
                        Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE, Database.Project.C_DONATIONAMOUNT}, new int[] {
                        R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org, R.id.btn_amount}));

    }

    public void onItemClick(AdapterView&lt;?&gt; l, View v, int position, long id) {
        Intent listIntent = new Intent(this, DetailsActivity.class);
        listIntent.putExtra("spendino.de.ProjectDetail.position",position);
        listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath(
                Provider.CONTENT_URI, Database.Project.NAME), Long
                .toString(id)));
        startActivity(listIntent);

    }

    /*
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.ui_menu_sync:
            doSync();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    void doSync() {
        Intent serviceIntent = new Intent(this, Sync.class);
        startService(serviceIntent);
    }


    class MySimpleCursorAdapter extends SimpleCursorAdapter {

        Context context=null;
        ImageLoader loader = null;
        private LayoutInflater mInflater = null;
        private int mProjectNameIndex = -1;
        private int mOrgNameIndex = -1;
        private int mDescIndex = -1;
        private int mDonationIndex = -1;
        private int mSmallImageIndex = -1;
        private int mKeywordIndex = -1;
        private int mPriceIndex = -1;
        private int mShortcodeIndex = -1;



        public MySimpleCursorAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            loader = new ImageLoader(context);
            this.context = context;

            mInflater = getLayoutInflater();    
            mProjectNameIndex = c.getColumnIndexOrThrow(Database.Project.C_PROJECTTITLE);
            mOrgNameIndex = c.getColumnIndexOrThrow(Database.Project.C_ORGANIZATIONTITLE);
            mDescIndex = c.getColumnIndexOrThrow(Database.Project.C_PROJECTDESCRIPTION);
            mDonationIndex = c.getColumnIndexOrThrow(Database.Project.C_DONATIONAMOUNT);
            mSmallImageIndex = c.getColumnIndexOrThrow(Database.Project.C_SMALLIMAGE);
            mKeywordIndex = c.getColumnIndexOrThrow(Database.Project.C_KEYWORD);
            mPriceIndex = c.getColumnIndexOrThrow(Database.Project.C_PRICE);
            mShortcodeIndex = c.getColumnIndexOrThrow(Database.Project.C_SHORTCODE);

        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View convertView = mInflater.inflate(R.layout.listitems, null);
            convertView.setClickable(true);
            convertView.setFocusable(true);
            convertView.setBackgroundResource(android.R.drawable.menuitem_background);
            //convertView.setOnItemClickListener(ProjectsList.this);

            convertView.setOnClickListener(new OnClickListener() {
                 public void onClick(View view) {

                     /*
                  Intent myIntent = new Intent(view.getContext(), DetailsActivity.class);
                  String position = null;
                myIntent.putExtra("spendino.de.ProjectDetail.position",position);
                  long id = 0;
                myIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath(
                            Provider.CONTENT_URI, Database.Project.NAME), Long
                            .toString(id)));
                  view.getContext().startActivity(myIntent);
                 }
                });*/

            } 
                  });   




            return convertView;
        }

        protected void onItemClick(AdapterView&lt;?&gt; l, View v, int position, long id) {
            Intent listIntent = new Intent(ProjectsList.this, DetailsActivity.class);
            listIntent.putExtra("spendino.de.ProjectDetail.position",position);
            listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath(
                    Provider.CONTENT_URI, Database.Project.NAME), Long
                    .toString(id)));
            startActivity(listIntent);}

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Get your views from 'view'
            TextView projectTextView = (TextView) view.findViewById(R.id.txt_title);
            TextView orgTextView = (TextView) view.findViewById(R.id.txt_org);
            TextView descTextView = (TextView) view.findViewById(R.id.txt_list_desc);
            ImageView someImageView = (ImageView) view.findViewById(R.id.image);
            Button donate_now = (Button) view.findViewById(R.id.btn_donate);
            Button btn_amount = (Button) view.findViewById(R.id.btn_amount);
            donate_now.setFocusable(true);
            donate_now.setClickable(true);
            // Set the data
            projectTextView.setText(cursor.getString(mProjectNameIndex));
            orgTextView.setText("von " + cursor.getString(mOrgNameIndex));
            descTextView.setText(Html.fromHtml(cursor.getString(mDescIndex)+"..."));
            btn_amount.setText("Summe Spende: " + cursor.getString(mDonationIndex)+" €");

            donate_now.setOnClickListener(new OnClickListener() {
                 public void onClick(View view) {


                     AlertDialog.Builder alertbox = new AlertDialog.Builder(view.getContext());
                                            alertbox.setTitle("SMS");
                                            alertbox.setMessage (Html.fromHtml(("Ihr SMS wird vorbereitet. Bitte bestätigen Sie mit 'Ok' und senden Sie danach die SMS ab! &lt;p&gt;&lt;small&gt;Sie helfen dem Projekt mit " +mPriceIndex + ". Betrag je SMS zzgl. Transport. Der Betrag abzüglich 17 Cent geht direkt an die Partner-Organisation.")));


                                            alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface arg0, int arg1) {
                                                    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                                                    sendIntent.putExtra("address", mShortcodeIndex); 
                                                    sendIntent.putExtra("sms_body", mKeywordIndex); 
                                                    sendIntent.setType("vnd.android-dir/mms-sms");
                                                    startActivity(sendIntent);
                                                }   

                                    });
                                            alertbox.show();

            } 
                  });   
            loader.DisplayImage(cursor.getString(mSmallImageIndex ), context, someImageView);      
        }


        public void setViewImage(ImageView v, String value) {
            v.setTag(value);
            loader.DisplayImage(value, context, v);
        }
    }


}
</code></pre>
