Home > Net >  compilation error translating from kotlin to java
compilation error translating from kotlin to java

Time:04-11

I am adapting an application to read qr codes that was in kotlin to java. I only have an error in this line: intentIntegrator.setDesiredBarcodeFormats((Collection)CollectionsKt.listOf("QR_CODE")); it launch this error: "Cannot resolve symbol 'CollectionsKt'". I don't know how to adapt that to java. It seems to be the list of qr code types it supports. The complete program is this:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    ImageButton qrButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        qrButton= findViewById(R.id.qr_button);
        qrButton.setOnClickListener(new View.OnClickListener() {
            public final void onClick(View it) {
                IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
                intentIntegrator.setDesiredBarcodeFormats((Collection)CollectionsKt.listOf("QR_CODE"));
                //intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
                intentIntegrator.initiateScan();
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        final IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
        if (result != null) {
            (new AlertDialog.Builder((Context)this)).setMessage((CharSequence)("Would you like to go to "   result.getContents()   '?')).setPositiveButton((CharSequence)"Yes", (android.content.DialogInterface.OnClickListener)(new android.content.DialogInterface.OnClickListener() {
                public final void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent("android.intent.action.WEB_SEARCH");
                    intent.putExtra("query", result.getContents());
                    MainActivity.this.startActivity(intent);
                }
            })).setNegativeButton((CharSequence)"No", (android.content.DialogInterface.OnClickListener)null).create().show();
        }
        //Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show();
    }
}

CodePudding user response:

In java Collections are stacks, queues, deques, lists and trees. You can set any collection of Strings. In your case, list are preferred. Change for :

intentIntegrator.setDesiredBarcodeFormats(List.of("QR_CODE"));

or

intentIntegrator.setDesiredBarcodeFormats(Arrays.asList("QR_CODE"));
  • Related