简体   繁体   English

Android/Java 问题。 这两种决策树有何不同?

[英]Android/Java question. How are these two decision trees different?

I'm very confused as to how these two decision trees are different.我对这两个决策树有何不同感到非常困惑。 I'm building an app that needs to decide which view to load based on the position selected from a ListView.我正在构建一个应用程序,它需要根据从 ListView 中选择的 position 来决定加载哪个视图。 I've tried to build the logic into a single controller module and found that the switch-case will cause a NullPointerException and FC while the if-else will work perfectly.我尝试将逻辑构建到单个 controller 模块中,发现 switch-case 会导致 NullPointerException 和 FC,而 if-else 会完美运行。 Can anyone enlighten me as to why?谁能告诉我为什么? I've got a strong background in C and C++ and am used to being able to easily re-write switches to if-else's and vice versa.我在 C 和 C++ 方面有很强的背景,并且习惯于能够轻松地将开关重写为 if-else,反之亦然。

Defined vars:定义的变量:

private final int VALUEA = 0;
private final int VALUEB = 1;
private final int VALUEC = 2;

Switch-case:开关盒:

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;
}

The block above will cause a NullPointerException.上面的块将导致 NullPointerException。

If-else:如果别的:

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{
}

This version works perfectly.这个版本完美运行。 Does the second block work because of some funky Java scoping rule that allows each branch of the decision tree to create and properly initialize the TextView in a way that the first block doesn't?第二个块是否工作是因为一些时髦的 Java 范围规则允许决策树的每个分支以第一个块没有的方式创建和正确初始化 TextView?

The TextView constructor requires a Context . TextView构造函数需要一个Context You can't just pass it null .你不能只通过它null Instead do:而是这样做:

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;
}

I'm guessing it's the line我猜是这条线

TextView t = new TextView(null);

that's the problem.那就是问题所在。 Is it legal to pass null to the TextView constructor?将 null 传递给TextView构造函数是否合法?

Without seeing the stack trace this is just a stab in the dark.在没有看到堆栈跟踪的情况下,这只是在黑暗中刺伤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM