Search code examples
androidandroid-activitytabsspinneractivitygroup

Cannot Inflate View While in ActivityGroup


I am using an ActivityGroup to open up Activity 2 from within Activity1, which lives under a TabHost. So I have a group of Tabs and Activity 1 is part of those, but when I click a button inside Activity 1 I want Activity 2 to open but still be under the tabs. This works fine using the following code:

    public void replaceContentView(String id, Intent newIntent) {
    View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
    this.setContentView(view);
} 

However, within Activity 2 I have a few spinners that inflate custom windows and call for using a SimpleCursorAdapter:

    @Override
    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
    super.newDropDownView(context, cursor, parent);
    View view = View.inflate(context, R.layout.custom_spinner_row, null);
    int labColumn = cursor.getColumnIndex("lab");
    String getLab = cursor.getString(labColumn);
    TextView lab = (TextView)view.findViewById(R.id.CustomSpinnerTitle);
    lab.setText(getLab);        
    return view;
    }

I am getting this error:

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

The reason I am getting this error is because Activity 2 is not actually running because I replaced the contentView of Activity 1 with 2 in order to display it under the tabs. But I don't know any other way to do that. Is there a way to run Activity 2 under my tabs by in the same way actually run the activity rather than just replacing the contentView of 1?


Solution

  • You are using ActivityGroup that deals with View, so when you are using ActivityGroup you cannot directly setContentView(R.layout.your_xml);. In that case you have to inflate your XML into View.

    So, replace setContentView(R.layout.your_xml) by

    View contentView = LayoutInflater.from(getParent()).inflate(R.layout.your_xml, null);
    setContentView(contentView);
    

    Hope this works for you.