Android的微调错误:android.view.WindowManager $ BadTokenException:无法添加窗口窗口、错误、android、Android

2023-09-12 21:51:58 作者:旧巷故人

我想用的String []或设置微调值的ArrayList。

I want to set the spinner value using String[] or ArrayList.

我已经做了微调工作fine.In本次活动的标签acivityGroup内的另一个选项卡的活动。

I have done spinner in other activity working fine.In this activity inside the Tab acivityGroup another Tab activity.

我的问题是设定值进入微调。微调是正常显示禅师是指当负载活动,这是工作正常,但是当我点击微调其报错:

My problem is setting values into spinner. Spinner is displaying correctly Thay means when load the activity , that is working fine but when I click On spinner its give error:

错误是:

    09-30 16:11:37.693: ERROR/AndroidRuntime(699): FATAL EXCEPTION: main
09-30 16:11:37.693: ERROR/AndroidRuntime(699): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@407f4de8 is not valid; is your activity running?
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.ViewRoot.setView(ViewRoot.java:527)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.app.Dialog.show(Dialog.java:241)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.widget.Spinner.performClick(Spinner.java:260)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.View$PerformClick.run(View.java:9080)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.os.Handler.handleCallback(Handler.java:587)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.os.Looper.loop(Looper.java:123)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at java.lang.reflect.Method.invoke(Method.java:507)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at dalvik.system.NativeStart.main(Native Method)

这是我的code:

   View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.line_discount, null);
    this.setContentView(viewToLoad); 

   ArrayList<String> productList = new ArrayList<String>();
    int size = products.size()+1;
    String[] proList = new String[size];
    proList[0] = "---Select----";

    for(int i = 1; i< size ;i++){
        productList.add(products.get(i-1).getDescription());
        proList[i] = products.get(i-1).getDescription();
    }

    sp = (Spinner)findViewById(R.id.spProList);
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (LineDiscountActivity.this, android.R.layout.simple_spinner_item, proList);
    sp.setAdapter(adapter);

这是我的形象:

问题的TabActivity.Because我遇到这部分内TabActivityGroup。它是working.When我运行此范围内TabActivityGroup的标签活动里面,那么它的一个问题。 我有TabActivtyGroup和放大器;在正常TabActivity

Problem in TabActivity.Because I have run this part Within the TabActivityGroup. Its was working.When I run this inside the Tab Activity within TabActivityGroup, then its a problem. I have TabActivtyGroup &Within that normal TabActivity.

如何才能做到在这种情况呢?

How can I do in this situation?

请人帮我。

感谢

推荐答案

我觉得你有背景problem.Try使用下面的方法来获取上下文

I think you have context problem.Try to get context using below method

您可以创建一个新的活动,并设置其主题对话主题所以,当你开始你的活动将显示为对话框。 有关对话框,请参考下文后

you can create a new activity and set its theme to dialog theme so that when you start your activity it will display as dialog. For more information about dialog see below post

Click这里

EDIT2

我发现 badTokenExcaption

在您的活动的的onCreate()方法按

In your activity's onCreate() method replace the line setContentView(R.layout.XXXXX) by

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.XXXXX, null);
this.setContentView(viewToLoad); 

和按照行替换微调code

and replace the spinner code by following lines

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.medicine_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spDosageType.setAdapter(adapter);