我是Android Studio的新手,我正在尝试构建一个简单的QR码扫描仪。但是,当我尝试构建时,出现以下错误:
failed app/src/main/java/com/example/myapplication/MainActivity.kt
Unresolved reference: new
Unresolved reference: initiateScan
Type mismatch: inferred type is Int but Intent! was expected
Type mismatch: inferred type is Int but Intent? was expected
我真的不确定要从哪里去修复它。任何帮助将不胜感激!这是我的代码:
//package com.example.myapplication
package com.example.vicky.qrcodescanner
import android.R.attr
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.R
import com.google.zxing.integration.android.IntentIntegrator
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
fun OnCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_scan.setOnClickListener {
new IntentIntegrator(this).initiateScan();
// val scanner = IntentIntegrator(activity:this)
//
// scanner.initiateScan()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(resultCode == Activity.RESULT_OK) {
val result =
IntentIntegrator.parseActivityResult(requestCode, resultCode, attr.data)
if (result != null) {
if (result.contents == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG)
.show()
}
} else {
super.onActivityResult(requestCode, resultCode, attr.data)
}
}
}
}
答案 0 :(得分:0)
更改
} else {
super.onActivityResult(requestCode, resultCode, data)
}
到
#Get the paths of the directories which contain train and test data
train_dir = os.path.join('/ImageClassifier/cats_and_dogs_filtered', 'train')
validation_dir = os.path.join('/ImageClassifier/cats_and_dogs_filtered', 'validation')
train_cats_dir = os.path.join(train_dir, 'cats') # directory with our training cat pictures
train_dogs_dir = os.path.join(train_dir, 'dogs') # directory with our training dog pictures
validation_cats_dir = os.path.join(validation_dir, 'cats') # directory with our validation cat pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs') # directory with our validation dog pictures
num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))
num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))
total_train = num_cats_tr + num_dogs_tr
total_val = num_cats_val + num_dogs_val
batch_size = 128
epochs = 15
IMG_HEIGHT = 150
IMG_WIDTH = 150
train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='binary')
val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
directory=validation_dir,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='binary')
model = Sequential([
Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
MaxPooling2D(),
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(512, activation='relu'),
Dense(1)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit_generator(
train_data_gen,
steps_per_epoch=total_train // batch_size,
epochs=epochs,
validation_data=val_data_gen,
validation_steps=total_val // batch_size
)