Android/Java问题。这两个决策树有何不同?
我很困惑这两个决策树是如何不同的。我正在构建一个应用程序,它需要根据从列表视图中选择的位置来决定加载哪个视图。我尝试将逻辑构建到单个控制器模块中,发现开关情况将导致NullPointerException和FC,而if-else将正常工作。有人能告诉我为什么吗?我有一个强大的背景C和C++,我习惯于能够轻松地重写开关到if,反之亦然。 定义变量:Android/Java问题。这两个决策树有何不同?,java,android,Java,Android,我很困惑这两个决策树是如何不同的。我正在构建一个应用程序,它需要根据从列表视图中选择的位置来决定加载哪个视图。我尝试将逻辑构建到单个控制器模块中,发现开关情况将导致NullPointerException和FC,而if-else将正常工作。有人能告诉我为什么吗?我有一个强大的背景C和C++,我习惯于能够轻松地重写开关到if,反之亦然。 定义变量: private final int VALUEA = 0; private final int VALUEB = 1; private final i
private final int VALUEA = 0;
private final int VALUEB = 1;
private final int VALUEC = 2;
开关箱:
TextView t = new TextView(null);
switch(value){
case VALUEA:
setContentView(R.layout.valuealayout);
t = (TextView) findViewById(R.id.valuealayout);
t.findViewById(R.id.valuealayout);
break;
case VALUEB:
setContentView(R.layout.valueblayout);
t = (TextView) findViewById(R.id.valueblayout);
t.findViewById(R.id.valueblayout);
break;
case VALUEC:
setContentView(R.layout.valueclayout);
t = (TextView) findViewById(R.id.valueclayout);
t.findViewById(R.id.valueclayout);
break;
default:
break;
}
上面的块将导致NullPointerException
否则:
if(value == VALUEA ){
setContentView(R.layout.valuealayout);
TextView t = (TextView) findViewById(R.id.valuealayout);
t.findViewById(R.id.valuealayout);
}else if(value == VALUEB){
setContentView(R.layout.valueblayout);
TextView t = (TextView) findViewById(R.id.valueblayout);
t.findViewById(R.id.valueblayout);
}else if(value == VALUEC){
setContentView(R.layout.valueclayout);
TextView t = (TextView) findViewById(R.id.valueclayout);
t.findViewById(R.id.valueclayout);
}else{
}
这个版本非常好用。第二个块工作是不是因为一些时髦的Java作用域规则,允许决策树的每个分支以第一个块不工作的方式创建并正确初始化TextView?我猜这是一行
TextView t = new TextView(null);
这就是问题所在。将null传递给TextView
构造函数合法吗
没有看到堆栈跟踪,这只是暗中捅了一刀。我猜这是一条线
TextView t = new TextView(null);
这就是问题所在。将null传递给TextView
构造函数合法吗
在看不到堆栈跟踪的情况下,这只是一种暗中操作。文本视图构造函数需要一个
上下文
。您不能只传递它null
。相反,你应该:
TextView t = null;
switch(value){
case VALUEA:
setContentView(R.layout.valuealayout);
t = (TextView) findViewById(R.id.valuealayout);
t.findViewById(R.id.valuealayout);
break;
case VALUEB:
setContentView(R.layout.valueblayout);
t = (TextView) findViewById(R.id.valueblayout);
t.findViewById(R.id.valueblayout);
break;
case VALUEC:
setContentView(R.layout.valueclayout);
t = (TextView) findViewById(R.id.valueclayout);
t.findViewById(R.id.valueclayout);
break;
default:
break;
}
TextView
构造函数需要上下文。您不能只传递它null
。相反,你应该:
TextView t = null;
switch(value){
case VALUEA:
setContentView(R.layout.valuealayout);
t = (TextView) findViewById(R.id.valuealayout);
t.findViewById(R.id.valuealayout);
break;
case VALUEB:
setContentView(R.layout.valueblayout);
t = (TextView) findViewById(R.id.valueblayout);
t.findViewById(R.id.valueblayout);
break;
case VALUEC:
setContentView(R.layout.valueclayout);
t = (TextView) findViewById(R.id.valueclayout);
t.findViewById(R.id.valueclayout);
break;
default:
break;
}
要对此进行扩展,为什么不使用TextView t代码>并等待在开关块中对其进行初始化?还有,为什么t.findViewById()
行在那里?首先:谢谢,是的,传递给构造函数的null现在有意义了。我想我没有很好地阅读这些文件。第二:我会接受你的建议,让交换机进行初始化,这样看起来更干净。第三:我是个白痴,哈哈。我把头撞在墙上有一段时间了——第二行findViewById不应该在那里。要扩展这个,为什么不直接用TextView t代码>并等待在开关块中对其进行初始化?还有,为什么t.findViewById()
行在那里?首先:谢谢,是的,传递给构造函数的null现在有意义了。我想我没有很好地阅读这些文件。第二:我会接受你的建议,让交换机进行初始化,这样看起来更干净。第三:我是个白痴,哈哈。我把头撞在墙上有一阵子——第二条findViewById线不应该在那里。