Android的Childactivity的getParent问题吗? [英] Android Childactivity getParent question?
问题描述
基本上我在我的应用程序中使用的ActivityGroup。我有这种情况:
我Tabhost具有活性的。
活动A创建childActivity B点。
A --->乙startChildActivity(CollectionList,新的意图(这一点,MyCollectionList.class));
活动B创建3 childactivities C,D。
B&--- GT; C(的B childActivity)
startChildActivity(推荐,新的意图(MyCollectionList.this,Recommended.class));
B ---电> D(的B childActivity)
startChildActivity(ExpectSoon,新的意图(MyCollectionList.this,ExpectSoon.class));
B创建另一个childActivity过,将其命名为如
B&--- GT; Ë
意向previewMessage =新意图(的getParent(),MyCollectionId.class);
TabGroupActivity parentActivity =(TabGroupActivity)的getParent();
parentActivity.startChildActivity(MyCollectionId,previewMessage);
所以基本上活动C和D可以开始动鄂也与
意向previewMessage =新意图(的getParent(),MyCollectionId.class);
TabGroupActivity parentActivity =(TabGroupActivity)的getParent();
parentActivity.startChildActivity(MyCollectionId,previewMessage);
我不得不重写onBack pressed方法,这样我就可以控制回button.It看起来是这样的:
私人的ArrayList<串GT; mIdList;@覆盖
公共无效onBack pressed(){
INT长度= mIdList.size();
如果(长度> = 1){
活动电流= getLocalActivityManager()getActivity(mIdList.get(长-1))。
current.finish();
}
}
所以我的问题是,当我在活动E和preSS后退按钮是我的应用程序close.And,我有另一个问题是,在动鄂警告对话框。
按钮停用=(按钮)findViewById(R.id.deactivate);
deactivate.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
新AlertDialog.Builder(的getParent())
.setTitle(警告)
.setMessage(该系列将完全从device.You删除后再试一次操作需要互联网连接可以重新激活它。)
.setPositiveButton(来吧,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
Log.d(AlertDialog,正);
}
})
.setNegativeButton(取消,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
Log.d(AlertDialog,负);
}
})
。显示(); } });
当我从开始活动E,当我点击按钮,会显示警告对话框,一切都ok.But当我从C或D开始动鄂它抛出我,异常:
15 08-15:48:22.819:ERROR / AndroidRuntime(32440):致命异常:主要
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):$ android.view.WindowManager BadTokenException:无法添加窗口 - 令牌android.app.LocalActivityManager$LocalActivityRecord@4630ea20无效;是您的活动运行?
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.view.ViewRoot.setView(ViewRoot.java:509)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.view.Window $ LocalWindowManager.addView(Window.java:424)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.app.Dialog.show(Dialog.java:241)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.app.AlertDialog $ Builder.show(AlertDialog.java:802)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在com.stampii.stampii.mystampii.MyCollectionId $ 4.onClick(MyCollectionId.java:75)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.view.View.performClick(View.java:2408)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.view.View $ PerformClick.run(View.java:8817)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.os.Handler.handleCallback(Handler.java:587)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.os.Handler.dispatchMessage(Handler.java:92)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.os.Looper.loop(Looper.java:144)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在android.app.ActivityThread.main(ActivityThread.java:4937)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在java.lang.reflect.Method.invokeNative(本机方法)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在java.lang.reflect.Method.invoke(Method.java:521)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-15 15:48:22.819:ERROR / AndroidRuntime(32440):在dalvik.system.NativeStart.main(本机方法)
编辑:
我startChildActivity看起来是这样的:
公共无效startChildActivity(字符串ID,意图意图){
。窗口的窗口= getLocalActivityManager()startActivity(ID,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
如果(窗口!= NULL){
mIdList.add(ID);
的setContentView(window.getDecorView());
}
}
最新LogCat中,而在使用ActivityE Recommended.parentActivity:
新AlertDialog.Builder(Recommended.parentActivity)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967年):致命异常:主要
08-15 16:33:53.509:ERROR / AndroidRuntime(1967年):显示java.lang.NullPointerException
。08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在com.android.internal.app.AlertController $ AlertParams<&初始化GT;(AlertController.java:743)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967年):在android.app.AlertDialog $生成器<&初始化GT;(AlertDialog.java:273)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在com.stampii.stampii.mystampii.MyCollectionId $ 4.onClick(MyCollectionId.java:62)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在android.view.View.performClick(View.java:2408)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在android.view.View $ PerformClick.run(View.java:8817)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在android.os.Handler.handleCallback(Handler.java:587)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在android.os.Handler.dispatchMessage(Handler.java:92)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在android.os.Looper.loop(Looper.java:144)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在android.app.ActivityThread.main(ActivityThread.java:4937)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在java.lang.reflect.Method.invokeNative(本机方法)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在java.lang.reflect.Method.invoke(Method.java:521)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-15 16:33:53.509:ERROR / AndroidRuntime(1967):在dalvik.system.NativeStart.main(本机方法)
您可以做onething
在ActivityA.java
公共静态活动parentActivity;的onCreate()
{
parentActivity =这一点;
}
//启动你的孩子的活动,即(E)
ActivityE.java
按钮停用=(按钮)findViewById(R.id.deactivate);
deactivate.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
新AlertDialog.Builder(ActivityA.parentActivity.this)
.setTitle(警告)
.setMessage(该系列将完全从device.You删除后再试一次操作需要互联网连接可以重新激活它。)
.setPositiveButton(来吧,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
Log.d(AlertDialog,正);
}
})
.setNegativeButton(取消,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
Log.d(AlertDialog,负);
}
})
。显示(); } });
Basically I'm using ActivityGroup in my application. I have this situation:
I have Tabhost with activity A.
Activity A creates childActivity B.
A ---> B
startChildActivity("CollectionList", new Intent(this,MyCollectionList.class));
Activity B creates 3 childactivities C, D.
B ---> C (childActivity of B)
startChildActivity("Recommended", new Intent(MyCollectionList.this,Recommended.class));
B ---> D (childActivity of B)
startChildActivity("ExpectSoon", new Intent(MyCollectionList.this,ExpectSoon.class));
B creates another childActivity too, name it E.
B ---> E
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("MyCollectionId", previewMessage);
So basically Activity C and D can start activity E too, with :
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("MyCollectionId", previewMessage);
I had Override the onBackPressed method so I can control the back button.It looks like this :
private ArrayList<String> mIdList;
@Override
public void onBackPressed () {
int length = mIdList.size();
if ( length >=1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
current.finish();
}
}
So my problem is that when I'm in activity E and press the back button my application close.And the other problem that I have is with the alert dialog in activity E.
Button deactivate = (Button) findViewById(R.id.deactivate);
deactivate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder( getParent() )
.setTitle( "Warning" )
.setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
.setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
}
})
.show();
}
});
When I start Activity E from A, when I click the button,which will show the alert dialog,everything is ok.But when I start activity E from C or D it throws me that exception :
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): FATAL EXCEPTION: main
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4630ea20 is not valid; is your activity running?
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.view.ViewRoot.setView(ViewRoot.java:509)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.app.Dialog.show(Dialog.java:241)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:75)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.view.View.performClick(View.java:2408)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.view.View$PerformClick.run(View.java:8817)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.os.Handler.handleCallback(Handler.java:587)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.os.Looper.loop(Looper.java:144)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at android.app.ActivityThread.main(ActivityThread.java:4937)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at java.lang.reflect.Method.invoke(Method.java:521)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-15 15:48:22.819: ERROR/AndroidRuntime(32440): at dalvik.system.NativeStart.main(Native Method)
EDIT:
My startChildActivity looks like this :
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
latest LogCat,while using Recommended.parentActivity in ActivityE :
new AlertDialog.Builder( Recommended.parentActivity )
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): FATAL EXCEPTION: main
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): java.lang.NullPointerException
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:62)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.view.View.performClick(View.java:2408)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.view.View$PerformClick.run(View.java:8817)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.os.Handler.handleCallback(Handler.java:587)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.os.Looper.loop(Looper.java:144)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at android.app.ActivityThread.main(ActivityThread.java:4937)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at java.lang.reflect.Method.invoke(Method.java:521)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): at dalvik.system.NativeStart.main(Native Method)
you can do onething in ActivityA.java
public static Activity parentActivity;
onCreate()
{
parentActivity=this;
}
// start your child activity ie(E)
ActivityE.java
Button deactivate = (Button) findViewById(R.id.deactivate);
deactivate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder( ActivityA.parentActivity.this )
.setTitle( "Warning" )
.setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
.setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
}
})
.show();
}
});
这篇关于Android的Childactivity的getParent问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!