优化控件初始化函数
authorCaisking <3449750381@qq.com>
Sat, 10 Aug 2024 04:23:58 +0000 (12:23 +0800)
committerCaisking <3449750381@qq.com>
Sat, 10 Aug 2024 04:23:58 +0000 (12:23 +0800)
code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp

index bc3df8d..6b80116 100644 (file)
@@ -31,9 +31,18 @@ namespace OHOS
     bool InitUILabel(UILabel *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize)
     {
         // 检查标签指针是否为空
-        label = new UILabel();
         if (label == nullptr)
         {
+            label = new UILabel();
+            if (label == nullptr)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is error");
+                return false; // 内存分配失败,返回false
+            }
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is not nullptr");
             return false; // 内存分配失败,返回false
         }
         // 设置标签的位置和大小
@@ -68,11 +77,18 @@ namespace OHOS
     bool InitLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize)
     {
         // 检查标签指针是否为空
-
-        label = new UILabelExt();
         if (label == nullptr)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error");
+            label = new UILabelExt();
+            if (label == nullptr)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error");
+                return false; // 内存分配失败,返回false
+            }
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr");
             return false; // 内存分配失败,返回false
         }
         // 设置标签的位置和大小
@@ -94,10 +110,18 @@ namespace OHOS
     bool InitLabelAdapt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize)
     {
         // 检查标签指针是否为空
-        label = new UILabelExt();
         if (label == nullptr)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is error");
+            label = new UILabelExt();
+            if (label == nullptr)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is error");
+                return false; // 内存分配失败,返回false
+            }
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt  is not nullptr");
             return false; // 内存分配失败,返回false
         }
         // 设置标签的位置和大小
@@ -133,10 +157,18 @@ namespace OHOS
     bool InitLabelExt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, const char *fontName, int fontSize)
     {
         // 检查标签指针是否为空
-        label = new UILabelExt();
         if (label == nullptr)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is error");
+            label = new UILabelExt();
+            if (label == nullptr)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is error");
+                return false; // 内存分配失败,返回false
+            }
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is not nullptr");
             return false; // 内存分配失败,返回false
         }
         // 设置标签的位置和大小
@@ -179,6 +211,11 @@ namespace OHOS
                 return false; // 内存分配失败,返回false
             }
         }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UILabelButtonExt UILabelButtonExt is not nullptr");
+            return false; // 内存分配失败,返回false
+        }
         button->SetPosition(left, top);
         button->Resize(width, height);
         button->SetViewId(viewId);
@@ -198,6 +235,7 @@ namespace OHOS
     {
         if (label == nullptr)
         {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateLabelText label is nullptr");
             return nullptr;
         }
         /*取Id对应的字符串*/
@@ -232,7 +270,6 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error");
             return nullptr;
         }
-
         std::string str = UpdateLabelText(label, std::to_string(num), id);
         return str;
     }
@@ -255,6 +292,7 @@ namespace OHOS
     {
         if (label == nullptr)
         {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error");
             return;
         }
 
@@ -285,10 +323,18 @@ namespace OHOS
     bool InitButton(UIButton *&button, int left, int top, int width, int height, const char *viewId)
     {
         // 创建新按钮(如果当前按钮为空)
-        button = new UIButton();
         if (button == nullptr)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "new UIButton fail");
+            button = new UIButton();
+            if (button == nullptr)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "new UIButton fail");
+                return false; // 内存分配失败,返回false
+            }
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is not nullptr");
             return false; // 内存分配失败,返回false
         }
         button->SetPosition(left, top);
@@ -314,11 +360,20 @@ namespace OHOS
      */
     bool InitImageView(UIImageView *&imageView, int left, int top, int width, int height, const char *viewId, const char *imagePath)
     {
-        imageView = new UIImageView();
+        // 创建新图片(如果当前图片为空)
         if (imageView == nullptr)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "imagePath %s fail", imagePath);
-            return false;
+            imageView = new UIImageView();
+            if (imageView == nullptr)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "imagePath %s fail", imagePath);
+                return false;
+            }
+        }
+        else
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is not nullptr");
+            return false; // 内存分配失败,返回false
         }
         if (imagePath == nullptr)
         {