我的應用程序突然停止了。我添加了一個tabhost,並在其中添加了一個對話框。因此,當我運行我的應用程序時,它突然停止,同時重定向到我所擁有的活動創建了我的標籤主機。我的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);
}
}
我已張貼上面請吧.. – user2656455
哪條線是Friends.java:57? – m0skit0
不要使用'getBaseContext'或'getApplicationContext()'來創建對話框。 使用'this'或'ActivityName.this'。 –