我的MainActivity代码空指针访问警告
private void updateConnectionState()
{
Device localDevice = this.Controller.getConnectedDevice();
if (localDevice == null)
updateModelSpinner(localDevice.getType()); //warning at this line
str2 = localDevice.getHostName();
if (!TextUtils.isEmpty(str2))
}
private void updateSpinner(Device.Type paramType)
{
boolean bool = Device.Type.UNKNOWN.equals(paramType);
int i = 0;
if (!bool)
i = 1 + paramTvType.ordinal();
this.ModelSpinner.setSelection(i);
}
private void ModelSpinner(Device.Type paramType)
{
boolean bool = Device.Type.UNKNOWN.equals(paramType);
int i = 0;
if (!bool)
i = 1 + paramType.ordinal();
this.ModelSpinner.setSelection(i);
}
我的枚举类是
public class Device {
private Type type = Type.A_LOGIC;
public static Type getTypeForId(int paramInt)
{
switch (paramInt)
{
default:
return Type.A_LOGIC;
case 0:
return TvType.B_LOGIC;
case 1:
return TvType.A_LOGIC;
case 2:
return TvType.D_LOGIC;
case 3:
return TvType.E_LOGIC;
case 4:
}
return TvType.F_LOGIC;
}
public void setType(Type paramType)
{
this.Type = paramType;
}
public enum Type
{
A_LOGIC("A_LOGIC"),
B_LOGIC ("B_LOGIC"),
C_LOGIC ("C_LOGIC"),
D_LOGIC("D_LOGIC"),
E_LOGIC ("E_LOGIC"),
UNKNOWN("UNKNOWN");
private String object;
TvType(String localobj)
{
this.object; = localobj;
}
public String getLetter()
{
return this.object;;
}
}
public Type getType()
{
return this.type;
}
,并在我的方法中的主要活动正在呼叫的类型
updateSpinner(localDevice.getType());
但这里它显示的警告为
Null pointer access: The variable localTVDevice can only be null at this location
并在该行抛出一个零点错误。 对于枚举的概念我不熟悉,所以请告诉我为什么抛出这个错误。我提到了堆栈溢出但找不到答案。
您应该共享您的updateSpinner方法。 –
和所有的调用代码。您发布的代码和您的错误消息引用了不同的变量名称,我们需要查看您实际编写的内容。 – chrylis
您可能希望使用带有数字字段的枚举,并在使用'object'的地方使用'name()'。同时检查你的开关'情况1'这里可能有一个错字('C_LOGIC') – 2014-01-07 06:26:19