9

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
Vivek
  • 4,170
  • 6
  • 36
  • 50
  • I'd recommend to build the tab in bites instead of doing it in one big chunk. Then you can pinpoint exacly what gives nullpointer. Nullpointer should be fairly simple to solve. – Warpzit Apr 28 '12 at 18:52
  • 1
    no actually actionBar itself is null if I apply theme `Theme.Dialog` hence the exception. – Vivek Apr 29 '12 at 11:47
  • My guess (its a guess) is that dialog theme doesn't have title and since actionbar deep down is a titlebar this means there is no actionbar hence null. Is it possible to use a different theme? – Warpzit Apr 29 '12 at 19:03
  • Could you post the complete exception stack? – ChrLipp May 03 '12 at 08:51
  • Remove the `windowNoTitle` property from the custom theme and try. – Dharmendra May 03 '12 at 19:28
  • 1
    @Dharmendra Same exception is coming after removing mention attribute – Vivek May 04 '12 at 06:29
  • See a possible solution: http://stackoverflow.com/questions/11425020/actionbar-in-a-dialogfragment – AlikElzin-kilaka Jan 03 '13 at 23:24

2 Answers2

1

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.

Community
  • 1
  • 1
Caio Faustino
  • 165
  • 1
  • 15
0

You can create a custom dialog theme by overriding the properties of dialog that you dont need for an action bar.. Also add the android:windowActionBar property to it.

<style name="Theme.CustomDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowActionBar">true</item>
    ...
</style>
Ron
  • 24,175
  • 8
  • 56
  • 97
  • If I apply this theme getting `java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout ` this error – Vivek May 02 '12 at 07:44
  • This should work. Are you sure you have put `parent="android:style/Theme.Dialog"` attribute in the style tag? try without the `windowNoTitle` item.. – Ron May 02 '12 at 08:38
  • Hey change the `Theme.dialog` to `android:Theme.Holo.Dialog` equivalent. This is the default Dialog theme for applications targeting Honeycomb or newer. `Theme.dialog` is reported as broken... – Ron May 02 '12 at 10:23