ฟีเจอร์ API สำหรับการปรับปรุงการจัดการข้อผิดพลาดในข้อมูลโดยย่อจะรวมอยู่ใน Android 15 หน้านี้จะแสดงแนวทางปฏิบัติที่ดีที่สุดเกี่ยวกับ API เหล่านี้
ใช้บล็อกคำสั่งลองติดรอบคอมโพเนนต์ที่ไม่ใช่คอมโพเนนต์ประกอบ
Compose ไม่อนุญาตบล็อกคำสั่งลองติดรอบ Composable แต่อนุญาตให้คุณรวม ตรรกะอื่นๆ ของแอปในบล็อกเหล่านี้ ซึ่งจะช่วยให้คุณใช้ Compose สำหรับ มุมมองข้อผิดพลาด ดังที่ปรากฏในตัวอย่างต่อไปนี้:
provideContent {
var isError = false;
var data = null
try {
val repository = (context.applicationContext as MyApplication).myRepository
data = repository.loadData()
} catch (e: Exception) {
isError = true;
//handleError
}
if (isError) {
ErrorView()
} else {
Content(data)
}
}
เลย์เอาต์ข้อผิดพลาดเริ่มต้น
หากมีข้อยกเว้นที่ตรวจไม่พบหรือเกิดข้อผิดพลาดในการเขียน ข้อมูลโดยย่อจะแสดง รูปแบบข้อผิดพลาดเริ่มต้น:
Glance ช่วยให้นักพัฒนาซอฟต์แวร์จัดเตรียมการจัดวาง XML เป็นการจัดวางสำรองได้หากการจัดองค์ประกอบ ล้มเหลว ซึ่งหมายความว่ามีข้อผิดพลาดในรหัสการเขียน UI ข้อผิดพลาดนี้ จะปรากฏขึ้นเช่นกันหากมีข้อผิดพลาดที่ไม่พบในโค้ดของแอป
class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout)
เลย์เอาต์นี้เป็นเลย์เอาต์แบบคงที่ซึ่งผู้ใช้โต้ตอบด้วยไม่ได้ แต่เลย์เอาต์นี้ก็มีคุณภาพดี ในกรณีฉุกเฉิน
เพิ่มการดำเนินการลงใน UI ข้อผิดพลาดเริ่มต้น
ในเวอร์ชัน Glance 1.1.0 Glance จะอนุญาตให้คุณลบล้างโค้ดการจัดการข้อผิดพลาดเริ่มต้นได้ ด้วยวิธีนี้ คุณจะสามารถเพิ่มการเรียกกลับการดำเนินการได้ในกรณีที่เกิดข้อยกเว้นที่ตรวจไม่พบหรือ ในการเขียน
หากต้องการใช้ฟีเจอร์นี้ ให้ลบล้างฟังก์ชัน onCompositionError()
:
GlanceAppWidget.onCompositionError(
context: Context,
glanceId: GlanceId,
AppWidgetId: Int,
throwable: Throwable
)
ในฟังก์ชันนี้ Glance จะกลับไปใช้ RemoteViews
API สำหรับการจัดการข้อผิดพลาด
ซึ่งจะช่วยให้คุณระบุเลย์เอาต์และเครื่องจัดการการดำเนินการโดยใช้ XML ได้
ตัวอย่างต่อไปนี้จะแสดงวิธีสร้าง UI ข้อผิดพลาดแบบทีละขั้นตอน มีปุ่มสำหรับส่งความคิดเห็น:
- เขียนไฟล์ error_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MyApplication.AppWidget.Error"
android:id="@android:id/background"
android:layout_width="match_parent"
android:textSize="24sp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/error_title_view"
android:layout_width="match_parent"
android:textColor="@color/white"
android:textFontWeight="800"
android:layout_height="wrap_content"
android:text="Example Widget Error" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="4dp"
android:layout_height="match_parent">
<ImageButton
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="center"
android:tint="@color/white"
android:id="@+id/error_icon"
android:src="@drawable/heart_broken_fill0_wght400_grad0_opsz24"
/>
<TextView
android:id="@+id/error_text_view"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp"
android:textSize="16sp"
android:layout_weight="1"
android:text="Useful Error Message!" />
</LinearLayout>
</LinearLayout>
- ลบล้างฟังก์ชัน
onCompositionError
override fun onCompositionError(
context: Context,
glanceId: GlanceId,
AppWidgetId: Int,
throwable: Throwable
) {
val rv = 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
ของคุณ
private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent {
val intent = Intent(context, UpgradeToHelloWorldPro::class.java)
intent.setAction("widgetError")
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}
- จัดการ Intent ใน
GlanceAppWidgetReceiver
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
Log.e("ErrorOnClick", "Button was clicked.");
}