Merge "Avoid NPE caused by SIM card without main menu"
am: 7f950ebf1b

Change-Id: Ie6803a895473eed46d27565b00a09eb5f8127fcf
diff --git a/src/com/android/stk/StkLauncherActivity.java b/src/com/android/stk/StkLauncherActivity.java
index a5271f6..8502c01 100755
--- a/src/com/android/stk/StkLauncherActivity.java
+++ b/src/com/android/stk/StkLauncherActivity.java
@@ -23,7 +23,6 @@
 import android.os.Bundle;
 import android.view.View;
 import android.view.KeyEvent;
-import android.view.Window;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
@@ -165,27 +164,34 @@
     }
 
     private int addStkMenuListItems() {
-        String appName = mContext.getResources().getString(R.string.app_name);
         StkAppService appService = StkAppService.getInstance();
-        String stkMenuTitle = null;
+        if (appService == null) {
+            return 0;
+        }
+
+        String appName = mContext.getResources().getString(R.string.app_name);
         String stkItemName = null;
         int simCount = TelephonyManager.from(mContext).getSimCount();
         mStkMenuList = new ArrayList<Item>();
 
         CatLog.d(LOG_TAG, "simCount: " + simCount);
         for (int i = 0; i < simCount; i++) {
-            //Check if the card is inserted.
+            // Check if the card is inserted.
             if (mTm.hasIccCard(i)) {
-                CatLog.d(LOG_TAG, "SIM " + i + " add to menu.");
-                mSingleSimId = i;
-                stkMenuTitle = appService.getMainMenu(i).title;
-                stkItemName = new StringBuilder(stkMenuTitle == null ? appName : stkMenuTitle)
-                    .append(" ").append(Integer.toString(i + 1)).toString();
-                Item item = new Item(i + 1, stkItemName, mBitMap);
-                item.id = i;
-                mStkMenuList.add(item);
+                Menu menu = appService.getMainMenu(i);
+                // Check if the card has a main menu.
+                if (menu != null) {
+                    CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " is add to menu.");
+                    mSingleSimId = i;
+                    stkItemName = new StringBuilder(menu.title == null ? appName : menu.title)
+                            .append(" ").append(Integer.toString(i + 1)).toString();
+                    Item item = new Item(i, stkItemName, mBitMap);
+                    mStkMenuList.add(item);
+                } else {
+                    CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " does not have main menu.");
+                }
             } else {
-                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
+                CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " is not inserted.");
             }
         }
         if (mStkMenuList != null && mStkMenuList.size() > 0) {