You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中ArrayList内容添加到TextView后显示为空的问题求助

解决SIM卡电话号码显示为空的问题

看起来你的代码逻辑框架没问题,但TextView显示空内容主要是权限未正确获取和getNumber()可能返回空值这两个核心原因导致的,下面一步步帮你排查解决:

1. 动态申请危险权限(API 23+必做)

Android 6.0(API级别23)及以上,READ_PHONE_STATE属于危险权限,仅在清单文件声明是不够的,必须在运行时动态申请权限。如果跳过这一步,系统会默认拒绝权限,导致getActiveSubscriptionInfoList()无法获取有效数据,或者返回的SubscriptionInfo中号码为空。

解决代码示例:

在MainActivity中补充权限申请逻辑:

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_READ_PHONE_STATE = 100;
    // ... 其他成员变量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        number = findViewById(R.id.phone_numbers); // 先初始化TextView

        // 优先处理权限逻辑
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    REQUEST_READ_PHONE_STATE);
        } else {
            // 权限已授予,正常获取SIM信息并更新UI
            GetCarriorsInformation();
            updatePhoneNumberTextView();
        }
    }

    // 处理权限申请结果回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_READ_PHONE_STATE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                GetCarriorsInformation();
                updatePhoneNumberTextView();
            } else {
                number.setText("需要授予读取电话状态权限才能显示号码");
            }
        }
    }

    // 抽离UI更新逻辑,避免重复代码
    private void updatePhoneNumberTextView() {
        if (Numbers == null || Numbers.isEmpty()) {
            number.setText("未获取到SIM卡号码");
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (String num : Numbers) {
            if (!TextUtils.isEmpty(num)) {
                sb.append(num).append(" , ");
            }
        }
        // 移除末尾多余的" , "
        if (sb.length() > 0) {
            sb.delete(sb.length() - 3, sb.length());
        }
        number.setText(sb.toString());
    }

    // ... 其他原有方法
}

2. 处理getNumber()返回空的情况

有些SIM卡的运营商并没有将电话号码写入SIM卡,或者系统无法通过SubscriptionInfo.getNumber()获取到号码。这时可以尝试通过TelephonyManager结合SIM卡槽索引来获取对应号码:

修改你的GetCarriorsInformation()方法:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
private void GetCarriorsInformation() {
    Numbers = new ArrayList<>();
    subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
    if (subInfoList == null) {
        return;
    }
    isMultiSimEnabled = subInfoList.size() > 1;

    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    for (SubscriptionInfo subscriptionInfo : subInfoList) {
        String simNumber = subscriptionInfo.getNumber();
        // 如果getNumber()为空,尝试通过TelephonyManager兜底获取
        if (TextUtils.isEmpty(simNumber)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // API 26+支持通过SubscriptionId获取对应卡槽的TelephonyManager
                TelephonyManager slotManager = telephonyManager.createForSubscriptionId(subscriptionInfo.getSubscriptionId());
                simNumber = slotManager.getLine1Number();
            } else {
                // 低版本尝试反射调用(部分设备兼容)
                try {
                    Method method = telephonyManager.getClass().getMethod("getLine1Number", int.class);
                    simNumber = (String) method.invoke(telephonyManager, subscriptionInfo.getSimSlotIndex());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        Numbers.add(TextUtils.isEmpty(simNumber) ? "未获取到号码" : simNumber);
    }
}

3. 额外注意事项

  • 尽量使用真实物理设备测试,模拟器大多无法模拟真实多SIM卡环境,返回的号码常为空。
  • 部分国产ROM有额外的隐私保护设置,即使代码申请了权限,也需要用户在系统设置中手动开启对应权限。

内容的提问来源于stack exchange,提问作者user8990420

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:26:38