public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String test = Util.imei();
}
import android.content.Context;
import android.telephony.TelephonyManager;
public class Util{
Context context;
public Util(Context context) {
this.context = context;
}
public String imei() {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
}
出现错误“无法从 Util 类型对非静态方法 imei() 进行静态引用”。如果我将行更改为:
public static String imei() {
...
static Context context;
我收到错误并崩溃应用程序。(“E/AndroidRuntime(629): Caused by:java.lang.NullPointerException”)