3

My MainActivity class:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
               ....
               ...
}

Style.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

When i run the app then it is working perfect but when i am trying to create/show AlertDialog.Builder from my adapter class then it gives error

 `You need to use a Theme.AppCompat theme (or descendant) with this activity.at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)`

Manifest file:

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

AlertDialog code:

public class mainviewAdapter  extends ArrayAdapter<MainCategory>
{

 AlertDialog.Builder builderSingle;
 public mainviewAdapter  (Context context, ArrayList<MainCategory> values) {
        super(context, R.layout.home_rt, values);

        this.context = context;
        this.values = values;
        builderSingle = new AlertDialog.Builder(context);
        builderSingle.setIcon(R.mipmap.ic_launcher);
        builderSingle.setTitle("Select Your Category");


    }

   on button click:

   builderSingle.setNegativeButton("cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builderSingle.setAdapter(arrayAdapter,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        builderSingle.show();

How can i solved this error on Alertdialog ?

1 Answers1

1

Try to set Theme to your AlertDialog by replacing your line of code :

builderSingle = new AlertDialog.Builder(context);

with :

builderSingle = new AlertDialog.Builder(context,R.style.dialogTheme);

Add code in your style.xml :

<style name="dialogTheme" parent="@style/Theme.AppCompat">
<!-- Any customizations for your app running on devices with Theme.Holo here -->
</style>

and remember one thing to avoid Unable to add window -- token null is not for an application Exception Instead of getApplicationContext(), just use Activityname.this

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
  • After try this code i am getting `Unable to add window -- token null is not for an application at android.view.ViewRootImpl.setView(ViewRootImpl.java:641)` error. –  Dec 30 '15 at 06:01
  • I have edit my Answer try it now it will work and use Activityname.this as context while creating object of your class MainviewAdapter – Kapil Rajput Dec 30 '15 at 06:49
  • It's because you are using the wrong context, try with ActivityName.this see this [answer](http://stackoverflow.com/a/7229248/2324122) – kito Jun 24 '16 at 14:06