0

I'm getting above error at dialog.setContentView(R.layout.alert_ok); this line even though "alert_ok" xml is exist in my app's "layout" folder. Below is my complete java code to display my custom alert--

public void showAlertOk(Context context, String title, String message,
                            String OkTitle) {
        // custom dialog
        final Dialog dialog = new Dialog(context, R.style.MyAlertDlgTheme);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.alert_ok);
        dialog.setCanceledOnTouchOutside(false);

        TextView alertTitle = (TextView) dialog.findViewById(R.id.alertTitle);
        alertTitle.setText(title);

        TextView tvAlertMsg = (TextView) dialog.findViewById(R.id.tvAlertMsg);
        tvAlertMsg.setText(message);

        Button btnAlertOK = (Button) dialog.findViewById(R.id.btnAlertOK);
        btnAlertOK.setText(OkTitle);

        btnAlertOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onfieldAlertListener.onOkClicked();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

Below is my "alert_ok.xml" file--

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/one_fifty"
    android:background="@drawable/rounded_white"
    android:padding="5dp" >

    <TextView
        android:id="@+id/alertTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:gravity="center"
        android:padding="5dp"
        android:text="@string/safenest"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="@dimen/twenty"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvAlertMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/llAlertBtn"
        android:layout_below="@+id/alertTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:text="Message"
        android:textColor="@android:color/black"
        android:textSize="15sp"
        android:textStyle="normal" />

    <LinearLayout
        android:id="@+id/llAlertBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@color/alert_devider"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnAlertOK"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/dlg_bg"
            android:gravity="center"
            android:paddingBottom="7dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="7dp"
            android:text="@string/ok"
            android:textColor="@color/alert_btn_color"
            android:textStyle="bold" />
    </LinearLayout>

    <View
        android:id="@+id/alertDivider_1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@id/llAlertBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@color/alert_devider" />

</RelativeLayout>

Below is how I'm calling showAlertOk() from my "RegisterNSocietyMobNoVerifyActivity"

public class RegisterNSocietyMobNoVerifyActivity extends AppCompatActivity
{


    CommonAlertHandler alertHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_new_mobno_verify_activity);

        alertHandler = new CommonAlertHandler();
        alertHandler.setWikiAlertListner(this);
        alertHandler.showAlertOk(RegisterNSocietyMobNoVerifyActivity.this, getResources().getString(R.string.safenest), getResources().getString(R.string.failed_msg), getResources().getString(R.string.ok));

     }
}

Below is my "MyAlertDlgTheme" code--

 <style name="MyAlertDlgTheme" parent="android:Theme.Dialog">

        <!-- Fill the screen -->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>

        <!-- Custom backgrounds, titles or window float -->
        <item name="android:windowBackground">@drawable/rounded_white</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

below is my "rounded_white" drawable code--

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="@color/dlg_bg" />

    <stroke
        android:width="3dip"
        android:color="@color/dlg_bg" />

    <corners android:radius="10dip" />

    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />

</shape>

For more information, please see my complete error log as below --

01-31 16:35:24.242 19275-19275/com.winsnit.societymanagement E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.winsnit.societymanagement, PID: 19275
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.winsnit.societymanagement/com.winsnit.societymanagement.RegisterNSocietyMobNoVerifyActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f0200d1
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:136)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5111)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0200d1
                                                                                   at android.content.res.Resources.getValue(Resources.java:1133)
                                                                                   at android.content.res.MiuiResources.getValue(MiuiResources.java:135)
                                                                                   at android.content.res.Resources.getDrawable(Resources.java:708)
                                                                                   at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3042)
                                                                                   at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3085)
                                                                                   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:286)
                                                                                   at android.app.Dialog.setContentView(Dialog.java:477)
                                                                                   at com.winsnit.societymanagement.Common.CommonAlertHandler.showAlertOk(CommonAlertHandler.java:79)
                                                                                   at com.winsnit.societymanagement.RegisterNSocietyMobNoVerifyActivity.onCreate(RegisterNSocietyMobNoVerifyActivity.java:94)
                                                                                   at android.app.Activity.performCreate(Activity.java:5248)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247) 
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:136) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5111) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
                                                                                   at dalvik.system.NativeStart.main(Native Method) 
01-31 16:38:55.822 932-3631/? E/GpsXtraDownloader: No XTRA servers were specified in the GPS configuration

                                                   [ 01-31 16:38:56.012 26436:26436 W/         ]
                                                   [ColorAdjust] gammamode=2, cemode=10


                                                   [ 01-31 16:38:56.012 26436:26436 W/         ]
                                                   [ColorAdjust] temp_gammavalue=2, temp_cevalue=10


                                                   [ 01-31 16:38:56.012 26436:26436 W/         ]
                                                   [ColorAdjust] Don't setGamma!


                                                   [ 01-31 16:38:56.012 26436:26436 W/         ]
                                                   [ColorAdjust] Don't setCe!


                                                   [ 01-31 16:38:56.012 26436:26436 W/         ]
                                                   [ColorAdjust] Set temp_prefer temp_ce!
01-31 16:40:24.492 932-986/? E/InputDispatcher: channel '424b6a28 com.winsnit.societymanagement/com.winsnit.societymanagement.Token_Activity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-31 16:43:55.862 932-3055/? E/GpsXtraDownloader: No XTRA servers were specified in the GPS configuration

Also let me know if I can provide more information for the same. Thank you.

Dnyanesh M
  • 1,349
  • 4
  • 18
  • 46

4 Answers4

1

I had the same problem, check if the version of the .xml drawable file was built in the same version of the others file.

Mine was (v.24), and was not working.

I had copy and paste a drawable file and changed the color in the code, but when I pasted the file on my drawable folder, the file was converted to (v.24).

I deleted the file and created a new empty drawable file, then I pasted the xml code inside this empty file. The (v.24) was gone and my resource worked just fine.

Hope it helps.

-1

Just make the following changes, Pass Activity reference instead of context. This worked for me.

final Dialog dialog = new Dialog(YourActivity.this, R.style.MyAlertDlgTheme);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Pratik
  • 25
  • 1
  • 4
-1

Dont use context or getApplicationContext, just pass YourActivity.this

final Dialog dialog = new Dialog(YourActivity.this, R.style.MyAlertDlgTheme);

Thank you

Abdul Ahad
  • 435
  • 4
  • 14
  • I tried ur solution but getting same error at same line. Thanks for your reply. – Dnyanesh M Feb 01 '17 at 06:18
  • Please make sure that your MyAlertDlgTheme is compatible and matches with the theme you are using for your app in the manifest – Abdul Ahad Feb 01 '17 at 06:23
  • Actually I have used same code in my 2 other projects, n it is working fine there. If you have any doubt about "MyAlertDlgTheme", please check my updated question. – Dnyanesh M Feb 01 '17 at 06:40
-1

Try this:

  1. In android studio Build-->Clean project.

  2. Run your project.

Vishal Jadav
  • 924
  • 9
  • 11