Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity dialog with tab

I am building application for tab.

  1. On click of button I am trying to show Activity (say A) with theme @android:style/Theme.Dialog
  2. Inside activity A I am creating actionbar and adding 4 tabs.
  3. On click of tab I want to change fragment.

Everything works fine until I am not applying theme @android:style/Theme.Dialog After applying theme I am getting exception:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ebuilder/com.ebuilder.views.MyTabActivity}: java.lang.NullPointerException

at following line:

actionBar.addTab(actionBar.newTab().setCustomView(R.layout.tab_home).
                    setTabListener(homeListener), 0);

I have two questions:

1. What is going wrong if I apply theme dialog?

2. Is my approach right?

Let me know if anybody know another approach.

Edit1: *Manifest file*

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vivek"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk  android:targetSdkVersion="14"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FragmentInsideFragmentTestActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.CustomDialog" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

res/values/style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
            <item name="android:windowNoTitle">false</item>
            <item name="android:windowActionBar">true</item>        
    </style>
</resources>

Edit2

05-03 21:42:10.580: D/AndroidRuntime(533): Shutting down VM
05-03 21:42:10.580: W/dalvikvm(533): threadid=1: thread exiting with uncaught exception (group=0x40014760)
05-03 21:42:10.600: E/AndroidRuntime(533): FATAL EXCEPTION: main
05-03 21:42:10.600: E/AndroidRuntime(533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vivek/com.vivek.FragmentInsideFragmentTestActivity}: java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.os.Looper.loop(Looper.java:132)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.ActivityThread.main(ActivityThread.java:4025)
05-03 21:42:10.600: E/AndroidRuntime(533):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 21:42:10.600: E/AndroidRuntime(533):  at java.lang.reflect.Method.invoke(Method.java:491)
05-03 21:42:10.600: E/AndroidRuntime(533):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-03 21:42:10.600: E/AndroidRuntime(533):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-03 21:42:10.600: E/AndroidRuntime(533):  at dalvik.system.NativeStart.main(Native Method)
05-03 21:42:10.600: E/AndroidRuntime(533): Caused by: java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout
05-03 21:42:10.600: E/AndroidRuntime(533):  at com.android.internal.app.ActionBarImpl.init(ActionBarImpl.java:214)
05-03 21:42:10.600: E/AndroidRuntime(533):  at com.android.internal.app.ActionBarImpl.<init>(ActionBarImpl.java:192)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.Activity.initActionBar(Activity.java:1767)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.Activity.setContentView(Activity.java:1781)
05-03 21:42:10.600: E/AndroidRuntime(533):  at com.vivek.FragmentInsideFragmentTestActivity.onCreate(FragmentInsideFragmentTestActivity.java:47)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-03 21:42:10.600: E/AndroidRuntime(533):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
05-03 21:42:10.600: E/AndroidRuntime(533):  ... 11 more
like image 548
Vivek Avatar asked Apr 23 '12 16:04

Vivek


1 Answers

I`m struggling with the same issue here and found two different approaches so far.

The first one described here is the one I used.

There was also this answer that looks very similar to the first.

The big problem seems to be that the ActionBar is only supposed to be used on a fullscreen Layout, so anything trying to apply a Dialog type layout will cause an exception. So that's why both of them are setting the Width at this.getWindow().setAttributes with a fixed value, to work around the issue.

That was the conclusion I came up with, hope it helps anyone else looking for this, since this is a very old question.

like image 198
Caio Faustino Avatar answered Oct 29 '22 13:10

Caio Faustino