这是我的代码
package com.javacodegeeks.androidqrcodeexample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class AndroidBarcodeQrExample extends Activity {
/** Called when the activity is first created. */
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
String disco = null;
String QRCode_result = null;
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = (WebView) findViewById(R.id.webView);
setContentView(R.layout.activity_main);
//PRENDO IL NOME UTENTE DALLA PRECEDENTE SCHEDA
Bundle extras = getIntent().getExtras();
if (extras != null) {
disco = extras.getString("Disco");
}
}
public void scanQR(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(AndroidBarcodeQrExample.this, "Nessuno Scanner Trovato", "Scarica subito uno Scanner", "Si", "No").show();
}
}
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
QRCode_result = contents;
//Prendo il primo numero
char Check = QRCode_result.charAt(0);
if (Check == '0') {
String url = "http://www.bparty.org/APP/localsite/CONNECT_ANDROID_QR.php?disco=" + disco + "&string=" + QRCode_result;
url = url.replace(" ", "%20");
Log.i("URL",url);
webView.loadUrl(url);
//ALLORA E' UN CODICE UNIVOCO LISTA
QRCode_result = "QRCode PER LISTA";
}
else if (Check == '1') {
// ALLORA E' UN CODICE DI TICKET COMPRATO
QRCode_result = "QRCode PER TICKET";
}
else {
//ALLORA NON ESISTE
QRCode_result = "QRCode NON VALIDO";
}
Toast toast = Toast.makeText(this, "Content:" + QRCode_result, Toast.LENGTH_LONG);
toast.show();
}
}
}
}
当我在qrcode扫描后运行应用程序时出现此错误:
06-15 03:43:27.809 11140-11140 / com.javacodegeeks.androidqrcodeexample E / AndroidRuntime:致命异常:主要 java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 0,result = -1,data = Intent { act = com.google.zxing.client.android.SCAN flg = 0x80000(有额外内容)}} 活动 {com.javacodegeeks.androidqrcodeexample / com.javacodegeeks.androidqrcodeexample.AndroidBarcodeQrExample}: 显示java.lang.NullPointerException
我认为这是因为网页视图未初始化良好.. 不是吗?
答案 0 :(得分:1)
在onCreate()
方法中,在webView
setContentView
初始化
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
.....
}