getLine1Number中的空检查结果无法解析或不是字段

时间:2013-10-11 17:19:32

标签: java android telephonymanager

我真的不确定为什么会这样。似乎getLine1Number没有被实例化 - 但似乎第二次引用它不需要它(当我注释掉null检查时它不会抛出任何错误。

WORKING:

public class StartActivity extends Activity implements OnClickListener {

    Button goButton;
    Context c;
    boolean isAirPlaneMode, isMDNPresent = false;//boolean values to check for airplane mode and if the sim populates the MDN
    int simState;
    TelephonyManager tm;
    boolean NetworkConnection = false;//boolean to check the Network Availability
    AlertDialog mConfirmAlert = null;
    TextView text;
    TextView mUpdatetext;
    int version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        version = android.os.Build.VERSION.SDK_INT;
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // to read the SIM state
        simState = tm.getSimState();
        System.out.println("Sim State" + simState);
        //if (tm.getLine1Number = null) {
            //isMDNPresent = true;
        //}
        // to check for MDN
        if (tm.getLine1Number().equalsIgnoreCase("")) {
            isMDNPresent = true;
        }

THROWS ERROR:无法解析getLine1Number或不是字段

public class StartActivity extends Activity implements OnClickListener {

    Button goButton;
    Context c;
    boolean isAirPlaneMode, isMDNPresent = false;//boolean values to check for airplane mode and if the sim populates the MDN
    int simState;
    TelephonyManager tm;
    boolean NetworkConnection = false;//boolean to check the Network Availability
    AlertDialog mConfirmAlert = null;
    TextView text;
    TextView mUpdatetext;
    int version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        version = android.os.Build.VERSION.SDK_INT;
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // to read the SIM state
        simState = tm.getSimState();
        System.out.println("Sim State" + simState);
        if (tm.getLine1Number = null) {
            isMDNPresent = true;
        }
        // to check for MDN
        if (tm.getLine1Number().equalsIgnoreCase("")) {
            isMDNPresent = true;
        }

3 个答案:

答案 0 :(得分:0)

应该是

    if (tm.getLine1Number() == null) {

注意()

答案 1 :(得分:0)

在工作部分,这是一种方法

if (tm.getLine1Number().

在非工作代码中,它用作变量(无“()”)

if (tm.getLine1Number = null)

您还想比较不初始化,因此请将其从

更改
if (tm.getLine1Number = null)

if (tm.getLine1Number() == null)

添加额外的“=”

答案 2 :(得分:0)

首先,你写了'tm.getLine1Number = null',好像它是一个字段。在第二个地方,你写了'tm.getLine1Number()。equalsIgnoreCase(“”),好像它是一个方法。这就是为什么一个工作而一个工作没有。