我坚持我的项目并请求你的帮助!
我正在尝试从MainActivity.java中的webview调用一个Activity(BarcodeScannerActivity.java),点击按钮使用javascript调用。我觉得我搞砸了某些地方的参数......我不确定我在哪里弄错了,我只是复制并修改了博客和stackoverflow中的代码。
我的Android Studio logcat中出现此错误:
[INFO:CONSOLE(315)]“未捕获错误:在NPObject上调用方法时出错。”,来源:http:// .........
这是我的服务器端html:
public static void main (String[] args) {
String points = "54.199997, 88.399216, 135.2, 250.09616, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 168.19669, 178.7, 207.49712, " +
"135.2, 168.19669, 370.1, 391.69785, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 370.1, 391.69785, 108.2, 115.39928, 135.2, 215.59856, " +
"135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 370.1, 391.69785, " +
"108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, " +
"108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 189.49884, " +
"135.2, 180.7966, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, " +
"108.2, 120.79874, 135.2, 217.09616, 135.2, 170.59763, 377.9, 391.6986, 108.2, 120.79874, 135.2, 208.99689, 135.2, 163.39717, 374.0, 391.69824, " +
"108.2, 120.79874, 135.2, 214.6982, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 374.0, 391.69824, " +
"135.2, 250.09616, 517.1, 544.6972, 108.2, 120.79874, 135.2, 198.79779, 135.2, 187.69778, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, " +
"135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 370.1, 391.69785, " +
"108.2, 120.79874, 135.2, 217.39865, 135.2, 175.69711, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 219.49155, " +
"135.2, 163.39717, 374.0, 391.69824, 108.2, 120.79874, 135.2, 201.19598, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 218.59897, " +
"135.2, 189.49573, 370.1, 391.69785, 108.2, 120.79874, 135.2, 218.59897, 135.2, 189.49573, 370.1, 391.69785, 108.2, 120.79874, 135.2, 209.5979, " +
"135.2, 163.39717, 370.1, 391.69785, 108.2, 120.79874, 135.2, 174.19609, 135.2, 172.69742, 370.1, 391.69785, 108.2, 120.79874, 135.2, 174.19609, " +
"135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 374.0, 391.69824, 108.2, 120.79874, 135.2, 214.39862, " +
"135.2, 178.9956, 374.0, 391.69824, 108.2, 120.79874, 135.2, 161.59735, 135.2, 196.39772, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, " +
"108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, " +
"108.2, 120.79874, 135.2, 195.1961, 135.2, 208.6958, 135.2, 178.0957, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 118.99892, " +
"135.2, 144.1991, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 118.99892, 135.2, 193.09724, 374.0, 391.69824, 108.2, 120.79874, " +
"135.2, 185.59799, 135.2, 189.19801, 377.9, 391.6986, 108.2, 120.79874, 135.2, 202.997, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, " +
"135.2, 217.69742, 135.2, 163.39717, 374.0, 391.69824, 135.2, 250.09616, 517.1, 544.6972, 108.2, 120.79874, 135.2, 187.69786, 135.2, 172.69742, " +
"370.1, 391.69785, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 120.79874, 135.2, 208.99646, 135.2, 182.8964, " +
"374.0, 391.69824, 108.2, 120.79874, 135.2, 214.6982, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 178.69823, 135.2, 163.39717, " +
"374.0, 391.69824, 108.2, 120.79874, 135.2, 178.69844, 135.2, 172.69742, 370.1, 391.69785, 108.2, 120.79874, 135.2, 178.69844, 135.2, 172.69742, " +
"377.9, 391.6986, 108.2, 120.79874, 135.2, 221.29475, 135.2, 163.39717, 374.0, 391.69824, 108.2, 120.79874, 135.2, 221.29475, 135.2, 163.39717, " +
"370.1, 391.69785, 524.7, 553.7995, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 440.6, 468.19724, 108.2, 125.59826, " +
"135.2, 179.2979, 187.7, 210.49771, 135.2, 208.69688, 370.1, 391.69785, 108.2, 120.79874, 135.2, 178.69844, 135.2, 172.69742, 374.0, 391.69824, " +
"517.1, 544.6972, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 436.7, 468.19687, 108.2, 118.698944, 135.2, " +
"196.99606, 135.2, 172.99727, 360.2, 391.69687, 108.2, 120.79874, 135.2, 201.19598, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, " +
"209.5979, 135.2, 163.39717, 370.1, 391.69785, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 374.0, 391.69824, 108.2, 120.79874, 135.2, " +
"208.69637, 135.2, 175.39758, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 115.39928, 135.2, " +
"215.59856, 135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 436.7, " +
"468.19687, 108.2, 118.698944, 135.2, 208.09802, 135.2, 162.79724, 364.1, 391.69724, 135.2, 250.09616, 517.1, 544.6972, 108.2, 120.79874, 135.2, " +
"178.0957, 135.2, 163.39717, 374.0, 391.69824, 108.2, 120.79874, 135.2, 185.59691, 135.2, 163.39717, 370.1, 391.69785, 108.2, 115.39928, 135.2, " +
"178.69844, 135.2, 172.69742, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 125.59826, 135.2, 179.2979, 187.7, 210.49771, 135.2, " +
"213.79517, 370.1, 391.69785, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 214.6982, 135.2, 172.69742, 370.1, 391.69785, 108.2, " +
"120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 180.79544, 135.2, 167.29678, 374.0, 391.69824, 108.2, " +
"120.79874, 135.2, 165.49696, 135.2, 167.29678, 374.0, 391.69824, 108.2, 120.79874, 135.2, 165.49696, 135.2, 167.29678, 374.0, 391.69824, 108.2, " +
"115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, " +
"115.39928, 135.2, 190.69446, 135.2, 190.69894, 377.9, 391.6986, 108.2, 115.39928, 135.2, 165.49861, 135.2, 195.19905, 377.9, 391.6986, 517.1, " +
"544.6972, 54.199997, 88.399216, 108.2, 118.99892, 135.2, 205.99895, 135.2, 195.79625, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, " +
"108.2, 118.99892, 135.2, 201.49861, 374.0, 391.69824, 108.2, 118.698944, 135.2, 201.79861, 135.2, 183.19757, 364.1, 391.69724, 517.1, 544.6972, " +
"54.199997, 87.499214, 108.2, 125.59826, 135.2, 182.2976, 190.7, 214.39763, 135.2, 215.897, 370.1, 391.69785, 517.1, 544.6972, 54.199997, " +
"87.499214, 108.2, 118.99892, 135.2, 211.99535, 370.1, 391.69785, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 364.1, 391.69724, 108.2, " +
"121.69865, 135.2, 191.89688, 135.2, 214.39604, 370.1, 391.69785, 108.2, 125.59826, 135.2, 222.19763, 135.2, 215.2975, 370.1, 391.69785, 108.2, " +
"115.39928, 135.2, 199.99884, 135.2, 193.99792, 377.9, 391.6986, 108.2, 115.39928, 135.2, 175.69751, 135.2, 166.39688, 374.0, 391.69824, 135.2, " +
"250.09616, 523.1, 544.6978, 108.2, 115.39928, 135.2, 198.797, 135.2, 171.1964, 374.0, 391.69824, 108.2, 115.39928, 135.2, 198.797, 135.2, " +
"171.1964, 377.9, 391.6986, 108.2, 115.39928, 135.2, 198.797, 135.2, 171.1964, 377.9, 391.6986, 523.1, 544.6978, 54.199997, 87.499214, 108.2, " +
"118.698944, 135.2, 174.19894, 135.2, 161.59735, 370.1, 391.69785, 108.2, 120.79874, 135.2, 178.69844, 135.2, 180.7966, 377.9, 391.6986, 108.2, " +
"120.79874, 135.2, 189.49884, 135.2, 180.7966, 370.1, 391.69785, 108.2, 115.39928, 135.2, 160.99742, 135.2, 195.79776, 377.9, 391.6986, 523.1, " +
"544.6978, 54.199997, 87.499214, 108.2, 120.79874, 135.2, 217.69742, 135.2, 163.39717, 370.1, 391.69785, 108.2, 120.79874, 135.2, 212.59627, " +
"135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 187.09769, 135.2, 163.39717, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, " +
"135.2, 172.69742, 377.9, 391.6986, 108.2, 120.79874, 135.2, 195.1961, 135.2, 209.29948, 135.2, 166.09691, 135.2, 173.89784, 135.2, 200.29836, " +
"370.1, 391.69785, 108.2, 118.698944, 135.2, 181.39537, 135.2, 193.09871, 377.9, 391.6986, 527.0, 544.69824, 54.199997, 87.499214, 108.2, " +
"125.59826, 135.2, 179.2979, 187.7, 211.39763, 135.2, 208.69688, 370.1, 391.69785, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, " +
"391.69824, 524.7, 553.7995, 54.199997, 87.499214, 108.2, 120.79874, 135.2, 216.79735, 135.2, 163.39717, 374.0, 391.69824, 108.2, 121.69865, " +
"135.2, 191.89688, 135.2, 214.39604, 440.6, 468.19724, 517.1, 544.6972";
String[] pointss = points.split(", ");
List<Column> columns = new ArrayList<> ();
for (int i = 0; i < pointss.length; i += 2) {
columns.add(new Column(Float.parseFloat(pointss[i]), Float.parseFloat(pointss[i+1])));
}
List<Column> columnsWithOverlap = columns.stream()
.distinct()
.sorted()
.collect(toList());
System.out.println(columnsWithOverlap);
}
这是我的包含webview的MainActivity.java:
<input type="button" value="Scanner" id="BtnScan" class="btn_dark" onclick="callActivity();" />
<script>
function callActivity() {
Android.openBarcodeScanner();
}
</script>
最后,想要被调用的BarcodeScannerActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
//getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
//getActionBar().hide();
setContentView(R.layout.activity_main);
MainWebView = (WebView) findViewById(R.id.mainWebview);
MainWebView.setWebViewClient(new WebViewClient());
//-- add javascript listener
MainWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
MainWebView.getSettings().setJavaScriptEnabled(true);
MainWebView.loadUrl(WebViewURL);
}
..skipped for brevity..
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void openBarcodeScanner(String str) {
Intent barcodeScanIntent = new Intent(mContext, BarcodeScannerActivity.class);
mContext.startActivity(barcodeScanIntent);
}
}
最后一个问题是,这个javascript代码也适用于我的情况吗? 或者我需要修改一下吗?
public class BarcodeScannerActivity extends Activity implements View.OnClickListener {
// private static final R = ;
// use a compound button so either checkbox or switch widgets work.
private CompoundButton autoFocus;
private CompoundButton useFlash;
private TextView statusMessage;
private TextView barcodeValue;
private static final int RC_BARCODE_CAPTURE = 9001;
private static final String TAG = "BarcodeMain";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barcode_scanner);
statusMessage = (TextView)findViewById(R.id.status_message);
barcodeValue = (TextView)findViewById(R.id.barcode_value);
autoFocus = (CompoundButton) findViewById(R.id.auto_focus);
useFlash = (CompoundButton) findViewById(R.id.use_flash);
findViewById(R.id.read_barcode).setOnClickListener(this);
}
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.read_barcode) {
// launch barcode activity.
Intent intent = new Intent(this, BarcodeCaptureActivity.class);
intent.putExtra(BarcodeCaptureActivity.AutoFocus, autoFocus.isChecked());
intent.putExtra(BarcodeCaptureActivity.UseFlash, useFlash.isChecked());
startActivityForResult(intent, RC_BARCODE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_BARCODE_CAPTURE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
statusMessage.setText(R.string.barcode_success);
barcodeValue.setText(barcode.displayValue);
Log.d(TAG, "Barcode read: " + barcode.displayValue);
} else {
statusMessage.setText(R.string.barcode_failure);
Log.d(TAG, "No barcode captured, intent data is null");
}
} else {
statusMessage.setText(String.format(getString(R.string.barcode_error),
CommonStatusCodes.getStatusCodeString(resultCode)));
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
提前非常感谢你!
答案 0 :(得分:1)
您的界面将字符串作为参数:
@JavascriptInterface
public void openBarcodeScanner(String str) {
但是你的调用并没有传递任何参数:
Android.openBarcodeScanner();