我知道我也有类似的问题,但是经过30分钟的研究,我只是想不通。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.erikb.vinliste, PID: 12950
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erikb.vinliste/com.example.erikb.vinliste.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.Dialog.<init>(Dialog.java:209)
at android.app.AlertDialog.<init>(AlertDialog.java:201)
at android.app.AlertDialog.<init>(AlertDialog.java:197)
at dmax.dialog.SpotsDialog.<init>(SpotsDialog.java:79)
at dmax.dialog.SpotsDialog.<init>(SpotsDialog.java:18)
at dmax.dialog.SpotsDialog$Builder.build(SpotsDialog.java:62)
at com.example.erikb.vinliste.MainActivity.onCreate(MainActivity.kt:43)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
进口:
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.InputFilter
import android.widget.Toast
import com.example.erikb.vinliste.Helper.RectOverlay
import com.google.firebase.ml.vision.FirebaseVision
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode
import com.google.firebase.ml.vision.common.FirebaseVisionImage
import com.wonderkiln.camerakit.*
import dmax.dialog.SpotsDialog
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.StringBuilder
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions
其余:
class MainActivity : AppCompatActivity() {
private lateinit var waiting_dialog:AlertDialog
override fun onResume() {
super.onResume()
camera_view.start()
}
override fun onPause() {
super.onPause()
camera_view.stop()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
requestedOrientation = (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
waiting_dialog = SpotsDialog.Builder().setMessage("Please wait").setCancelable(false).build()
btn_detect.setOnClickListener {
camera_view.start()
camera_view.captureImage()
graphic_overlay.clear()
}
camera_view.addCameraKitListener(object:CameraKitEventListener{
override fun onVideo(p0: CameraKitVideo?) {
}
override fun onEvent(p0: CameraKitEvent?) {
}
override fun onImage(p0: CameraKitImage?) {
waiting_dialog.show()
var bitmap:Bitmap = p0!!.bitmap
bitmap = Bitmap.createScaledBitmap(bitmap,camera_view.width,camera_view.height,false)
camera_view.stop()
runDetector(bitmap)
}
override fun onError(p0: CameraKitError?) {
}
})
}
private fun runDetector(bitmap: Bitmap?) {
val image = FirebaseVisionImage.fromBitmap(bitmap!!)
val options = FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(
FirebaseVisionBarcode.FORMAT_QR_CODE,
FirebaseVisionBarcode.FORMAT_CODABAR
)
.build()
val barcodeDetector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)
barcodeDetector.detectInImage(image)
.addOnSuccessListener { result-> processResult(result) }
.addOnFailureListener{ e-> Toast.makeText(this,e.message,Toast.LENGTH_SHORT).show()}
}
private fun processResult(result: List<FirebaseVisionBarcode>) {
for(item in result) {
var bounds = item.boundingBox
var raw_value = item.rawValue
var value_type = item.valueType
//Draw rectangle
val rect = RectOverlay(graphic_overlay,bounds)
graphic_overlay.add(rect)
//Show info
when(value_type)
{
FirebaseVisionBarcode.TYPE_TEXT -> {
val alertDialog = AlertDialog.Builder(this)
alertDialog.setMessage(raw_value)
alertDialog.setPositiveButton("OK",{dialog, which -> dialog.dismiss()})
val dialog = alertDialog.create()
dialog.show()
}
FirebaseVisionBarcode.TYPE_URL -> {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(raw_value))
startActivity(intent)
}
FirebaseVisionBarcode.TYPE_CONTACT_INFO -> {
val name = item.contactInfo!!.name!!.formattedName
val address = item.contactInfo!!.addresses[0].addressLines[0]
val email = item.contactInfo!!.emails[0].address
val info = StringBuilder("Name: ").append(name).append("\n")
.append("Address: ").append(address).append("\n")
.append("Emal: ").append(email).toString()
val alertDialog = AlertDialog.Builder(this)
alertDialog.setMessage(info)
alertDialog.setPositiveButton("OK",{dialog, which -> dialog.dismiss()})
val dialog = alertDialog.create()
dialog.show()
}
}
}
waiting_dialog.dismiss()
}
}
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* gradle plugin from the resource data it found. It
* should not be modified by hand.
*/
package dmax.dialog;
public final class R {
private R() {}
public static final class attr {
private attr() {}
public static final int DialogSpotColor = 0x7f020000;
public static final int DialogSpotCount = 0x7f020001;
public static final int DialogTitleAppearance = 0x7f020002;
public static final int DialogTitleText = 0x7f020003;
}
public static final class color {
private color() {}
public static final int spots_dialog_color = 0x7f04005b;
}
public static final class dimen {
private dimen() {}
public static final int progress_margin = 0x7f05006a;
public static final int progress_width = 0x7f05006b;
public static final int spot_size = 0x7f05006c;
public static final int title_margin = 0x7f05006d;
public static final int title_padding = 0x7f05006e;
}
public static final class drawable {
private drawable() {}
public static final int dmax_spots_spot = 0x7f060068;
}
public static final class id {
private id() {}
public static final int dmax_spots_progress = 0x7f07003f;
public static final int dmax_spots_title = 0x7f070040;
}
public static final class layout {
private layout() {}
public static final int dmax_spots_dialog = 0x7f09001d;
}
public static final class style {
private style() {}
public static final int SpotsDialogDefault = 0x7f0c00bc;
}
public static final class styleable {
private styleable() {}
public static final int[] Dialog = { 0x7f020000, 0x7f020001, 0x7f020002, 0x7f020003 };
public static final int Dialog_DialogSpotColor = 0;
public static final int Dialog_DialogSpotCount = 1;
public static final int Dialog_DialogTitleAppearance = 2;
public static final int Dialog_DialogTitleText = 3;
}
}
答案 0 :(得分:0)
我不得不为您创建“对话框”构建器的库。似乎在构建SpotsDialog
时没有设置上下文。确保在您的SpotsDialog.Builder
中添加上下文。确保您也没有丢失任何其他内容,例如库文档中的示例:
val dialog: AlertDialog = SpotsDialog.Builder()
.setContext(this)
.setMessage(R.string.custom_title)
.setCancelable(false)
.build()
在此处查看库参考以了解更多信息:https://github.com/d-max/spots-dialog