应用AlertDialog时,应用程序崩溃了

时间:2011-11-14 10:58:44

标签: android

我有一个列表视图,包含从数据库中填充。我在每一行上放了一个按钮,当它点击它时会显示一个AlertDialog框。

我的项目没有错误,但每次单击AlertDialog框的按钮时,应用程序都会崩溃。

这是我的代码:

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! 
                        <p><small>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();

有人可以帮我分析哪里出错了?

这是调试:

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)

以下是完整的活动代码:

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<?> 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<?> 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! <p><small>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);
        }
    }


}

2 个答案:

答案 0 :(得分:1)

您需要为alertDialog提供正确的上下文,该上下文必须是当前活动上下文。因此,您需要声明活动的上下文。

//At the beginning of class where you declare the Listview
ListView lv;
Context ctx = this;

在你的方法中。

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


                 AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
                                        alertbox.setTitle("SMS");
                                        alertbox.setMessage (Html.fromHtml(("Ihr SMS wird vorbereitet. Bitte bestätigen Sie mit 'Ok' und senden Sie danach die SMS ab! <p><small>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();

        } 
              });   

答案 1 :(得分:0)

最简单的解决方案是调用 getParent()而不是 ProjectsList.this 。根据{{​​3}},它似乎完全正常。

donate_now.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(getParent());
alertbox.setTitle("SMS");
alertbox.setMessage (Html.fromHtml(("Ihr SMS wird vorbereitet. Bitte 
                    bestätigen Sie mit 'Ok' und senden Sie danach die SMS ab! 
                    <p><small>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();

我在我的代码上测试了它(大部分是相同的)并且它完美地工作!!