android:飞行模式下的空指针异常
我正在制作一个应用程序,在其中我必须检查设备是否处于飞行模式,我使用以下代码,它是空指针异常。我的代码如下:android:飞行模式下的空指针异常,android,nullpointerexception,airplane,Android,Nullpointerexception,Airplane,我正在制作一个应用程序,在其中我必须检查设备是否处于飞行模式,我使用以下代码,它是空指针异常。我的代码如下: public static boolean isAirplaneModeOn(Context context) { System.out.println("test1"); return Settings.System.getInt(context.getContentResolver(),Settings.Sys
public static boolean isAirplaneModeOn(Context context)
{
System.out.println("test1");
return Settings.System.getInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
/**
*
* @param status
*/
public void setAirplaneMode(Context context,boolean status)
{System.out.println("test111");
boolean isAirplaneModeOn = isAirplaneModeOn(context);// **null pointer exception**
if(isAirplaneModeOn && status)
{
return;
}
if(!isAirplaneModeOn && !status)
{
return;
}
if(isAirplaneModeOn && !status)
{
Settings.System.putInt(getApplicationContext().getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", 0);
getApplicationContext().sendBroadcast(intent);
return;
}
if(!isAirplaneModeOn && status)
{
Settings.System.putInt(getApplicationContext().getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 1);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", 1);
getApplicationContext().sendBroadcast(intent);
return;
}
}
03-13 14:57:04.507: I/System.out(9185): test111
03-13 14:57:04.507: I/System.out(9185): test1
03-13 14:57:04.515: I/System.out(9185): java.lang.NullPointerException
有人能告诉我如何获得mAirplaneEnabled标志值吗
我的日志如下:
public static boolean isAirplaneModeOn(Context context)
{
System.out.println("test1");
return Settings.System.getInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
/**
*
* @param status
*/
public void setAirplaneMode(Context context,boolean status)
{System.out.println("test111");
boolean isAirplaneModeOn = isAirplaneModeOn(context);// **null pointer exception**
if(isAirplaneModeOn && status)
{
return;
}
if(!isAirplaneModeOn && !status)
{
return;
}
if(isAirplaneModeOn && !status)
{
Settings.System.putInt(getApplicationContext().getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", 0);
getApplicationContext().sendBroadcast(intent);
return;
}
if(!isAirplaneModeOn && status)
{
Settings.System.putInt(getApplicationContext().getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 1);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", 1);
getApplicationContext().sendBroadcast(intent);
return;
}
}
03-13 14:57:04.507: I/System.out(9185): test111
03-13 14:57:04.507: I/System.out(9185): test1
03-13 14:57:04.515: I/System.out(9185): java.lang.NullPointerException
npe是由上下文引起的,因为上下文在这里为null。因此代码段可能如下所示:
public boolean isAirplaneModeOn(Context context)
{
return Settings.System.getInt(ClassName.this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0)!=0;
}
对于startok njzk2,您不应该在android中使用System.out,但您可以通过查看NPE中的空值来帮助我解决问题。然后解释您要查找的mAirplaneEnabled标志值是什么。context.getContentResolver,上下文为null?idottiger,给我一些解决方案,用什么来代替上下文