تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتوفّر ميزات واجهة برمجة التطبيقات لتحسين معالجة الأخطاء على Glance بدءًا من Android 15. تقدّم هذه الصفحة أفضل الممارسات المتعلّقة بواجهات برمجة التطبيقات هذه.
استخدام كتلة try-catch حول المكوّنات غير القابلة للإنشاء
لا يسمح Compose باستخدام عبارات try-catch حول العناصر القابلة للإنشاء، ولكنّه يتيح لك تضمين منطق تطبيقك الآخر في هذه العبارات. يتيح لك ذلك استخدام Compose لعرض الأخطاء، كما هو موضّح في المثال التالي:
في حال حدوث استثناء لم يتم اكتشافه أو خطأ في Compose، يعرض Glance تنسيق خطأ تلقائيًا:
الشكل 1. تنسيق الخطأ التلقائي في Glance 1.0الشكل 2. تنسيق الخطأ التلقائي في الإصدار 1.1.0 من Glance
تتيح Glance للمطوّرين توفير تنسيق XML كخيار احتياطي في حال تعذُّر إنشاء التنسيق. وهذا يعني أنّه حدث خطأ في رمز Compose. تظهر واجهة المستخدم هذه الخاصة بالأخطاء أيضًا إذا كان لديك خطأ لم يتم رصده في رمز تطبيقك.
هذا التنسيق هو تنسيق ثابت لا يمكن للمستخدم التفاعل معه، ولكنّه مفيد في حالات الطوارئ.
الشكل 3. مثال على تخطيط خطأ مخصّص
إضافة إجراءات إلى واجهة المستخدم التلقائية للخطأ
بدءًا من الإصدار 1.1.0 من Glance، يمكنك تجاهل رمز معالجة الخطأ التلقائي.
بهذه الطريقة، يمكنك إضافة عمليات ردّ عند حدوث استثناء غير معالج أو خطأ في التركيب.
لاستخدام هذه الميزة، عليك إلغاء الدالة onCompositionError():
overridefunonCompositionError(context:Context,glanceId:GlanceId,appWidgetId:Int,throwable:Throwable){valrv=RemoteViews(context.packageName,R.layout.error_layout)rv.setTextViewText(R.id.error_text_view,"Error was thrown. \nThis is a custom view \nError Message: `${throwable.message}`")rv.setOnClickPendingIntent(R.id.error_icon,getErrorIntent(context,throwable))AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId,rv)}
أنشئ Intent معلّقًا يشير إلى GlanceAppWidgetReceiver:
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-21 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-21 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Handle errors with Glance\n\nAPI features for improving error handling on Glance are included beginning in\nAndroid 15. This page provides best practices regarding these APIs.\n\nUse a try-catch block around non-composable components\n------------------------------------------------------\n\nCompose doesn't allow try-catch blocks around composables, but lets you wrap\nyour app's other logic in these blocks. This lets you use Compose for your\nerror view, as shown in the following example: \n\n provideContent {\n var isError = false;\n var data = null\n try {\n val repository = (context.applicationContext as MyApplication).myRepository\n data = repository.loadData()\n } catch (e: Exception) {\n isError = true;\n //handleError\n }\n\n if (isError) {\n ErrorView()\n } else {\n Content(data)\n }\n }\n\nDefault error layout\n--------------------\n\nIf there is an uncaught exception or a Compose error, Glance displays a\ndefault error layout:\n**Figure 1.**Glance 1.0 default error layout **Figure 2.**Glance 1.1.0 default error layout\n\nGlance lets developers provide an XML layout as a fallback if composition\nfails. This means that there was an error in the Compose code. This error UI\nalso appears if you have an uncaught error in your app's code. \n\n class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout)\n\nThis layout is a static layout that your user can't interact with, but is good\nin emergency cases.\n**Figure 3.**Example custom error layout\n\nAdd actions to the default error UI\n-----------------------------------\n\nAs of Glance 1.1.0, Glance lets you override the default error handling code.\nThis way, you can add action callbacks in the event of an uncaught exception or\nerror in composition.\n\nTo use this feature, override the `onCompositionError()` function: \n\n GlanceAppWidget.onCompositionError(\n context: Context,\n glanceId: GlanceId,\n appWidgetId: Int,\n throwable: Throwable\n )\n\nIn this function, Glance falls back to the `RemoteViews` API for error handling.\nThis lets you specify layouts and action handlers using XML.\n\nThe following examples show you, step-by-step, how to create an error UI that\nincludes a button to send feedback:\n\n1. Write the `error_layout.xml` file:\n\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n style=\"@style/Widget.MyApplication.AppWidget.Error\"\n android:id=\"@android:id/background\"\n android:layout_width=\"match_parent\"\n android:textSize=\"24sp\"\n android:layout_height=\"match_parent\"\n android:orientation=\"vertical\"\u003e\n\n \u003cTextView\n android:id=\"@+id/error_title_view\"\n android:layout_width=\"match_parent\"\n android:textColor=\"@color/white\"\n android:textFontWeight=\"800\"\n android:layout_height=\"wrap_content\"\n android:text=\"Example Widget Error\" /\u003e\n\n \u003cLinearLayout\n android:layout_width=\"match_parent\"\n android:orientation=\"horizontal\"\n android:paddingTop=\"4dp\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cImageButton\n android:layout_width=\"64dp\"\n android:layout_height=\"64dp\"\n android:layout_gravity=\"center\"\n android:tint=\"@color/white\"\n android:id=\"@+id/error_icon\"\n android:src=\"@drawable/heart_broken_fill0_wght400_grad0_opsz24\"\n /\u003e\n \u003cTextView\n android:id=\"@+id/error_text_view\"\n android:layout_width=\"wrap_content\"\n android:textColor=\"@color/white\"\n android:layout_height=\"wrap_content\"\n android:layout_gravity=\"center\"\n android:padding=\"8dp\"\n android:textSize=\"16sp\"\n android:layout_weight=\"1\"\n android:text=\"Useful Error Message!\" /\u003e\n \u003c/LinearLayout\u003e\n\n \u003c/LinearLayout\u003e\n\n2. Override the `onCompositionError` function:\n\n override fun onCompositionError(\n context: Context,\n glanceId: GlanceId,\n appWidgetId: Int,\n throwable: Throwable\n ) {\n val rv = RemoteViews(context.packageName, R.layout.error_layout)\n rv.setTextViewText(\n R.id.error_text_view,\n \"Error was thrown. \\nThis is a custom view \\nError Message: `${throwable.message}`\"\n )\n rv.setOnClickPendingIntent(R.id.error_icon, getErrorIntent(context, throwable))\n AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, rv)\n }\n\n3. Create a pending intent that references your `GlanceAppWidgetReceiver`:\n\n private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent {\n val intent = Intent(context, UpgradeToHelloWorldPro::class.java)\n intent.setAction(\"widgetError\")\n return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)\n }\n\n4. Handle the intent in your `GlanceAppWidgetReceiver`:\n\n override fun onReceive(context: Context, intent: Intent) {\n super.onReceive(context, intent)\n Log.e(\"ErrorOnClick\", \"Button was clicked.\");\n }"]]