【发布时间】:2018-11-17 14:52:24
【问题描述】:
我正在尝试使用 JAVA 从字符串中获取 URL。但我的变量在“Uri.parse”部分不起作用(变量中没有值)。请考虑我是编码初学者
错误是:“无法为最终变量‘result’赋值”
我的代码:
showResultDialogue(result.getContents());
..
public void showResultDialogue(final String result) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
Pattern p = Pattern.compile("[\\w\\.]+\\.(?:com|cc|net|ru|in)[^,\\s]*");
Matcher m = p.matcher(result);
builder.setTitle("Example Title")
.setMessage("Text is " + result);
if(m.matches()) {
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(result) // here is problem
);
startActivity(browserIntent);
}
});
}
【问题讨论】:
-
错误堆栈跟踪是什么?
-
@SamzSakerz 'result' 变量中没有任何内容
-
如果有错误日志,您可以编辑您的问题吗?并将您在结果中输入的内容发送给我。
-
@SamzSakerz 输入是 "globalist.in/somthing.html" 。正则表达式匹配成功,但没有在浏览器中打开 url
-
我明白了,你可以试试我的建议,看看是否有效?