更改活动以查找WindowManager $ BadTokenException:

时间:2017-03-04 13:50:44

标签: android android-activity alertdialog android-windowmanager

我最近在tabhost中添加了新标签,并将代码从tab3移到了tab4,所有这些都没问题,直到我点击按钮找到此错误。

这些按钮都显示出某种对话 - 所以问题无疑在这里。

                                                            android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@2f93937 is not valid; is your activity running?
                                                                 at android.view.ViewRootImpl.setView(ViewRootImpl.java:849)
                                                                 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
                                                                 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
                                                                 at android.app.Dialog.show(Dialog.java:350)
                                                                 at android.app.Activity.showDialog(Activity.java:3782)
                                                                 at android.app.Activity.showDialog(Activity.java:3731)
                                                                 at com.example.tabdemo.Tab4Activity$2.onClick(Tab4Activity.java:100)
                                                                 at android.view.View.performClick(View.java:5697)
                                                                 at android.widget.TextView.performClick(TextView.java:10826)
                                                                 at android.view.View$PerformClick.run(View.java:22526)
                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                 at android.os.Looper.loop(Looper.java:158)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是代码

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Tab4Activity extends Activity implements ValueListener
{

    Button eta,arrive,complete;

    private int pHour;
    private int pMinute;

    /** This integer will uniquely define the dialog to be used for displaying time picker.*/

    static final int TIME_DIALOG_ID = 0;

    /** Callback received when the user "picks" a time in the dialog */

    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    pHour = hourOfDay;
                    pMinute = minute;
                    updateDisplay();
                    newETA=eta.getText().toString();
                    displayToast();


                    task="pETA";

                    // if job has been selected go and get the ETA
                    if(MainActivity.job_id>0)
                    {
                        BackgroundTask backgroundTaskUpdateETA = new BackgroundTask(Tab4Activity.this);
                        backgroundTaskUpdateETA.execute(task,""+MainActivity.job_id,newETA,"avail");
                    }
                }
            };

    /** Updates the time in the TextView */
    private void updateDisplay()
    {

        eta.setText(
                new StringBuilder()
                        .append(pad(pHour)).append(":")
                        .append(pad(pMinute)));
    }

    /** Displays a notification when the time is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(eta.getText()),   Toast.LENGTH_SHORT).show();

    }

    /** Add padding to numbers less than ten */
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }



    /////////////////////////////////
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab4);


        eta =(Button)findViewById(R.id.etaButton);
        arrive = (Button)findViewById(R.id.arrivedButton);
        complete = (Button)findViewById(R.id.completeJobButton);




        eta.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                showDialog(TIME_DIALOG_ID);
            }
        });


        //click listerner for arrived button
        arrive.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                pickupAlert();
            }
        });



        complete.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                completeAlert();
            }
        });


        /** Display the current time in the TextView */
        updateDisplay();


    }
//////////////////////

    public void pickupAlert()
    {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            { switch (which)
            {
                case DialogInterface.BUTTON_POSITIVE: // Yes button clicked

//do something
                    break;
                case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing

                    break;
            }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Arrived At Pickup?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener)
                .show();
    }


    public void completeAlert()
    {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            { switch (which)
            {
                case DialogInterface.BUTTON_POSITIVE: // Yes button clicked
                    task="complete";
                    BackgroundTask backgroundTaskArrived = new BackgroundTask(Tab4Activity.this);
                    backgroundTaskArrived.execute(task,""+MainActivity.job_id,"Complete","avail");
                    MainActivity.job_id=0;

                    break;
                case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing
                    Toast.makeText(Tab4Activity.this, "No Clicked", Toast.LENGTH_LONG).show();
                    break;
            }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Close This Fare?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener)
                .show();
    }
/////////////////////


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                        mTimeSetListener, pHour, pMinute, false);
        }
        return null;
    }
}

如果有任何帮助,我将不胜感激。

我已将问题缩小到

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Arrived At Pickup?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener)
            **.show();**

0 个答案:

没有答案