简体   繁体   English

开关-大小写表达式必须是常量表达式,我不能使用if-else语句

[英]Switch - case expressions must be constant expressions, I can't make the if-else statment

This is a project of QR Code generation and this is the source of the code That is the source here an error of the constant case I know that it must put in If-else but I don't know how in this project anyone can help me ?! 这是QR代码生成的项目,这是代码的源代码。 这是恒定大小写的错误,我知道它必须放在If-else中,但是我不知道在这个项目中任何人都可以提供帮助我 ?!

//That is a QR code generator project`enter code here`
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
          case R.id.button1://<--- is the error .. I can't make it If-else statement
           EditText qrInput = (EditText) findViewById(R.id.QR);
           String qrInputText = qrInput.getText().toString();
           Log.v(LOG_TAG, qrInputText);

           //Find screen size
           WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
           Display display = manager.getDefaultDisplay();
           Point point = new Point();
           display.getSize(point);
           int width = point.x;
           int height = point.y;
           int smallerDimension = width < height ? width : height;
           smallerDimension = smallerDimension * 3/4;

           //Encode with a QR Code image
           QREncoder qrCodeEncoder = new QREncoder(qrInputText, 
                     null, 
                     Contents.Type.TEXT,  
                     BarcodeFormat.QR_CODE.toString(), 
                     smallerDimension);
           try {
            Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
            ImageView myImage = (ImageView) findViewById(R.id.imageView1);
            myImage.setImageBitmap(bitmap);

           } catch (WriterException e) {
            e.printStackTrace();
           }


           break;

           // More buttons go here (if any) ...

          }
         }
    }

Take a look at the official blog post about this: 看看有关此的官方博客文章:

http://tools.android.com/recent/switchstatementconversion http://tools.android.com/recent/switchstatementconversion

http://tools.android.com/tips/non-constant-fields http://tools.android.com/tips/non-constant-fields

Basically, resource constants in library projects are no longer "final". 基本上,图书馆项目中的资源常量不再是“最终的”。 From the ADT Site: 在ADT网站上:

In other words, the constants are not final in a library project. 换句话说,常数在库项目中不是最终的。 The reason for this is simple: When multiple library projects are combined, the actual values of the fields (which must be unique) could collide. 原因很简单:合并多个库项目时,字段的实际值(必须唯一)可能会发生冲突。 Before ADT 14, all fields were final, so as a result, all libraries had to have all their resources and associated Java code recompiled along with the main project whenever they were used. 在ADT 14之前,所有字段都是最终字段,因此,所有库在使用时都必须与主项目一起重新编译其所有资源和关联的Java代码。 This was bad for performance, since it made builds very slow. 这对性能不利,因为它使构建速度非常慢。 It also prevented distributing library projects that didn't include the source code, limiting the usage scope of library projects. 它还阻止分发不包含源代码的库项目,从而限制了库项目的使用范围。

so if you the the fix it will convert switch into if and else... 因此,如果您修复此问题,它将把switch转换为if and else ...

int id = view.getId();
if (id == R.id.button1) {
    action1();
} else if (id == R.id.button2) {
    action2();
} else if (id == R.id.button3) {
    action3();
}

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

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