即使" alert_ok"我在dialog.setContentView(R.layout.alert_ok);
这条线上犯了错误。 xml存在于我的应用"布局"夹。下面是我完整的Java代码,用于显示我的自定义警报 -
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();
}
以下是我的" alert_ok.xml" 文件 -
<?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>
以下是我如何从我的&#34; RegisterNSocietyMobNoVerifyActivity&#34;
调用showAlertOk()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));
}
}
以下是我的&#34; MyAlertDlgTheme&#34;代码 -
<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>
下面是我的&#34; rounded_white&#34;可绘制代码 -
<?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>
有关详细信息,请参阅我的完整错误日志,如下所示 -
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
如果我可以提供相同的更多信息,请告诉我。谢谢。
答案 0 :(得分:0)
我遇到了同样的问题,请检查.xml可绘制文件的版本是否与其他文件的版本相同。
我是(v.24),并且没有工作。
我已经复制并粘贴了一个可绘制文件,并更改了代码中的颜色,但是当我将该文件粘贴到可绘制文件夹中时,该文件被转换为(v.24)。
我删除了文件并创建了一个新的空可绘制文件,然后将xml代码粘贴到该空文件中。 (v.24)不见了,我的资源也很好用。
希望有帮助。
答案 1 :(得分:-1)
只需进行以下更改,传递活动参考而不是上下文。这对我有用。
final Dialog dialog = new Dialog(YourActivity.this, R.style.MyAlertDlgTheme);
答案 2 :(得分:-1)
不要使用context或getApplicationContext,只需传递YourActivity.this
即可final Dialog dialog = new Dialog(YourActivity.this, R.style.MyAlertDlgTheme);
谢谢
答案 3 :(得分:-1)
试试这个:
在android studio Build - &gt; Clean project。
运行您的项目。