0

我的应用程序突然停止。我添加了一个选项卡主机,并在其中添加了一个对话框。所以当我运行我的应用程序时,它在重定向到我创建选项卡主机的活动时突然停止。我的 logcat 出现错误,有人可以帮我找到解决方案吗?

08-06 10:46:34.273: E/AndroidRuntime(1112): FATAL EXCEPTION: main
08-06 10:46:34.273: E/AndroidRuntime(1112): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neochat/com.neochat.Friends}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.os.Handler.dispatchMessage(Handler.java:99)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.os.Looper.loop(Looper.java:137)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.ActivityThread.main(ActivityThread.java:5041)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at java.lang.reflect.Method.invokeNative(Native Method)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at java.lang.reflect.Method.invoke(Method.java:511)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at dalvik.system.NativeStart.main(Native Method)

08-06 10:46:34.273: E/AndroidRuntime(1112): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)


08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.Dialog.show(Dialog.java:281)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at com.neochat.Friends.onCreate(Friends.java:57)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.Activity.performCreate(Activity.java:5104)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

08-06 10:46:34.273: E/AndroidRuntime(1112):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

08-06 10:46:34.273: E/AndroidRuntime(1112):     ... 11 more

班级

  import android.app.AlertDialog;
   import android.app.TabActivity;
    import android.content.DialogInterface;
     import android.content.Intent;
      import android.os.Bundle;
       import android.widget.TabHost;
         import android.widget.TabHost.TabSpec;
        import android.widget.Toast;


    @SuppressWarnings("deprecation")
      public class Friends extends TabActivity {

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

    TabHost tabHost = getTabHost();


    TabSpec homespec = tabHost.newTabSpec("Friends");
    // setting Title and Icon for the Tab
   homespec.setIndicator("", getResources().getDrawable(R.drawable.icon_friend_tab));
    Intent FriendsIntent = new Intent(this,Friends_list.class);
    homespec.setContent(FriendsIntent);

    TabSpec inboxspec = tabHost.newTabSpec("Chatroom");
    inboxspec.setIndicator("", getResources().getDrawable(R.drawable.icon_chat_tab));
    Intent ChatIntent = new Intent(this,InboxActivity.class);
    inboxspec.setContent(ChatIntent);

    TabSpec composespec = tabHost.newTabSpec("Trash");        
    composespec.setIndicator("",  
     getResources().getDrawable(R.drawable.icon_trash_tab));

    AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(getBaseContext());
    alertDialogBuilder.setMessage("Delete
        Converastion?").setCancelable(false).setPositiveButton("YES", new 
        DialogInterface.OnClickListener() {

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

            Toast.makeText(Friends.this, "Conversation Deleted
               !",Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("NO",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            // TODO Auto-generated method stub
            dialog.cancel();
        }
    } );

    AlertDialog alertdialog=alertDialogBuilder.create();
    alertdialog.show();





    //Intent DelIntent = new Intent(this, ComposeActivity.class);
    //composespec.setContent(DelIntent);

    TabSpec morespec =tabHost.newTabSpec("more");
    morespec.setIndicator("",getResources().getDrawable(R.drawable.icon_more_tab));
    Intent moreint=new Intent(this,More.class);
    morespec.setContent(moreint);


    // Adding all TabSpec to TabHost
    tabHost.addTab(homespec); 
    tabHost.addTab(composespec); 
    tabHost.addTab(inboxspec); 
   tabHost.addTab(morespec);
    }
    }
4

3 回答 3

0

将您的上下文更改为 ActivityName.this 或 getApplicationContext() 在您的

AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(getBaseContext());
于 2013-08-06T11:14:45.993 回答
0

尝试将 Builder 的构造函数更改为:

新的 AlertDialog.Builder(this);

于 2013-08-06T11:13:15.003 回答
0

似乎您Activity在定义Intent.

该行:

Intent FriendsIntent = new Intent(this,Friends_list.class);

应该

Intent FriendsIntent = new Intent(this,Friends.class);
于 2013-08-06T11:10:29.167 回答