为什么错误?调试和代码显示 - 在模拟器中运行

时间:2021-04-25 21:01:36

Okay, so I'm really getting angry with these sudden errors im coming through. Its just really I never experienced them before. I run in logcat and its showing me the error on the line but i dont know why its giving an error!

好的,所以我真的对这些突然发生的错误感到生气。它真的我从来没有经历过它们。我在logcat中运行它显示我在线上的错误,但我不知道为什么它给出一个错误!

Here is the full image:

这是完整的图像:

为什么错误?调试和代码显示 - 在模拟器中运行.

Here is the code:

这是代码:

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    {
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if("000000000000000".equals(tm.getDeviceId())) {
            Toast.makeText(this, "Emulator Worked", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "Emulator Failed", Toast.LENGTH_LONG).show();
        } 
    }
}

Anyone can please point out the problem that im failing to see? If i used "try", i just get more unexplained errors popping up in Eclipse.

任何人都可以指出我未能看到的问题?如果我使用“try”,我只会在Eclipse中弹出更多无法解释的错误。

2 个解决方案

#1


1  

Have you declared the permission

你有没有宣布这个许可

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

in manifest file

在清单文件中

#2


1  

The reason you are getting an exception thrown is because the line

您抛出异常的原因是因为该行

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

cannot be called before the activity's onCreate() method is called by your application. If you look at your parentheses, you will see that the call is outside the onCreate() method and therefore gets run during object instantiation. Correct your parentheses so that the TelephonyManager is created inside of onCreate().

在应用程序调用activity的onCreate()方法之前无法调用。如果查看括号,您将看到调用在onCreate()方法之外,因此在对象实例化期间运行。更正括号,以便在onCreate()内创建TelephonyManager。

In other words:

换一种说法:

public class MainActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
        if("000000000000000".equals(tm.getDeviceId())) { 
            Toast.makeText(this, "Emulator Worked", Toast.LENGTH_LONG).show(); 
        } 
        else { 
            Toast.makeText(this, "Emulator Failed", Toast.LENGTH_LONG).show(); 
        }  
    } 
} 

#1


1  

Have you declared the permission

你有没有宣布这个许可

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

in manifest file

在清单文件中

#2


1  

The reason you are getting an exception thrown is because the line

您抛出异常的原因是因为该行

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

cannot be called before the activity's onCreate() method is called by your application. If you look at your parentheses, you will see that the call is outside the onCreate() method and therefore gets run during object instantiation. Correct your parentheses so that the TelephonyManager is created inside of onCreate().

在应用程序调用activity的onCreate()方法之前无法调用。如果查看括号,您将看到调用在onCreate()方法之外,因此在对象实例化期间运行。更正括号,以便在onCreate()内创建TelephonyManager。

In other words:

换一种说法:

public class MainActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
        if("000000000000000".equals(tm.getDeviceId())) { 
            Toast.makeText(this, "Emulator Worked", Toast.LENGTH_LONG).show(); 
        } 
        else { 
            Toast.makeText(this, "Emulator Failed", Toast.LENGTH_LONG).show(); 
        }  
    } 
}