提问人:Vasant Raval 提问时间:8/2/2023 更新时间:8/2/2023 访问量:78
尝试在使用 evaluateJavascript 时在空对象引用上调用虚拟方法“boolean java.lang.String.equals(java.lang.Object)”
Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference , while using evaluateJavascript
问:
我试图从 webview evalueteJavascript 值中获取一些字符串返回
idk 什么是 wron,直到昨天它都完美运行,我没有对此代码进行任何更改,但不知道为什么它给我这个错误
尝试调用虚拟方法“boolean” java.lang.String.equals(java.lang.Object)' 在空对象引用上
我什至没有在这里使用任何布尔值,我有点困惑这里到底出了什么问题,请谁能帮我
主导航方法必须评估Javascript,其中第一个检查线程页面是否有效或损坏
表示页面的检查包含一个 elemnt,该 elemnt 主要出现在损坏的页面或私人个人资料中var brokenPage = document.querySelector('html[id=instagram]');
这两个
"var splashScreenDark = document.querySelector('html[class=" + selectorsdark + "]');" +
"var splashScreenLite = document.querySelector('html[class=" + selectorslite + "]');" +
检查该页面是否有效且不是私人帐户,或者是否是不是话题网页的意外页面
SEOCND 评估 JavaScript 有一个主要的 elemnt 检查,它区分评论页面和帖子页面
如果为 true,则表示它是评论页面,如果为 false,则表示是帖子页面document.querySelector('[data-pressable-container=true]');
public void linkValidationAndUrl(String url) throws IOException {
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));
textStatus.setText(getString(R.string.validating_link));
if (url.startsWith("https://www.threads.net") || url.startsWith("https://threads.net")) {
MainNav(url, new AccountStatusCallback() {
@Override
public void onAccountStatusReceived(String[] accountStatus) {
// Regex pattern for profile link
Pattern profileLinkPattern = Pattern.compile("^https://(?:www\\.)?threads\\.net/@[^/]+$");
Pattern post1Pattern = Pattern.compile("(?<=.net\\/t\\/).*(?=\\/\\?)|(?<=post\\/).*");
Matcher profileMatcher = profileLinkPattern.matcher(url);
Matcher post1Matcher = post1Pattern.matcher(url);
if (accountStatus[0].equals("true")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Broken Url or Private Profile", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.brokenorprivateStr));
} else if (accountStatus[0].equals("unexpected")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.unexpected_error));
} else if (accountStatus[0].equals("false")) {
if (profileMatcher.matches()) {
textStatus.setText(null);
textStatus.setText(getString(R.string.profile_link));
Intent iPr = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(iPr);
} else if (post1Matcher.find()) {
textStatus.setText(null);
if (accountStatus[1].equals("true")) {
//Navigate to comment page
textStatus.setText("Comment Link Provided");
Intent iC = new Intent(getApplicationContext(), CommentActivity.class);
startActivity(iC);
} else if (accountStatus[1].equals("false")) {
// Navigate to post page
textStatus.setText("Post Link Provided");
Intent iP = new Intent(getApplicationContext(), PostActivity.class);
startActivity(iP);
} else if (accountStatus[1].equals("unexpected")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.unexpected_error));
}
} else {
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadlink_invaliddataurl));
}
}else {
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText("Unable To Get Data");
}
}
});
} else if ((!url.startsWith("https://")) && (url.contains("www.threads.net") || url.contains("threads.net"))) {
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadLink_NoHttps));
} else {
Toast.makeText(MainActivity.this, "Invailed Url", Toast.LENGTH_SHORT).show();
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadLink_False));
}
}
public void MainNav(String profileUrl, AccountStatusCallback callback) {
final String[] AccountAndCommentStatus = {null, null};
String selectorsdark = "\"_9dls __fb-dark-mode\"";
String selectorslite = "\"_9dls __fb-light-mode\"";
AtomicInteger evaluationCounter = new AtomicInteger(); // Define the evaluationCounter here
webView.loadUrl(profileUrl);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// Delay the JavaScript execution by 2000 milliseconds (2 seconds)
// Execute JavaScript to get the account status
new Handler().postDelayed(() -> {
webView.evaluateJavascript("javascript: var brokenPage = document.querySelector('html[id=instagram]');\n" +
"var splashScreenDark = document.querySelector('html[class=" + selectorsdark + "]');" +
"var splashScreenLite = document.querySelector('html[class=" + selectorslite + "]');" +
"var pageAvailability = 'null';\n" +
"if (brokenPage) {\n" +
" pageAvailability = 'true';\n" +
"}else if (splashScreenDark || splashScreenLite){\n" +
" pageAvailability = 'false'\n" +
"}else{pageAvailability = 'null'}\n" +
"pageAvailability;", value -> {
if (value != null) {
// Remove the double quotes around the value
value = value.replace("\"", "");
switch (value) {
case "true":
AccountAndCommentStatus[0] = "true";
break;
case "false":
AccountAndCommentStatus[0] = "false";
break;
case "null":
AccountAndCommentStatus[0] = "unexpected";
break;
}
// Increase the evaluation counter
evaluationCounter.getAndIncrement();
// Check if both evaluations are done
if (evaluationCounter.get() == 2 && callback != null) {
callback.onAccountStatusReceived(AccountAndCommentStatus);
}
}else {
Toast.makeText(getApplicationContext(),"Unexpected Error Main Page",Toast.LENGTH_LONG).show();
}
});
}, 2000);
new Handler().postDelayed(() -> {
// Execute JavaScript to get the comment status
webView.evaluateJavascript("javascript: var pageAvail = document.querySelector('html[id=instagram]');\n" +
"var commentExist = 'null';\n" +
"if (!pageAvail) {\n" +
"\n" +
" var firstData = document.querySelector('[data-pressable-container=true]');\n" +
" if (firstData) {\n" +
" var repliesElement = firstData.querySelector('svg[aria-hidden=true][fill=none]');\n" +
" if (repliesElement) {\n" +
" commentExist = 'true';\n" +
" } else { commentExist = 'false';}\n" +
" }\n" +
"}\n" +
"commentExist;\n", value -> {
if (value != null) {
// Remove the double quotes around the value
value = value.replace("\"", "");
switch (value) {
case "true":
AccountAndCommentStatus[1] = "true";
break;
case "false":
AccountAndCommentStatus[1] = "false";
break;
case "null":
AccountAndCommentStatus[1] = "unexpected";
break;
}
// Increase the evaluation counter
evaluationCounter.getAndIncrement();
// Check if both evaluations are done
if (evaluationCounter.get() == 2 && callback != null) {
callback.onAccountStatusReceived(AccountAndCommentStatus);
}
}else {
Toast.makeText(getApplicationContext(),"Unexpected Error Post/Comment Page",Toast.LENGTH_LONG).show();
}
});
}, 4000);
}
});
}
public interface AccountStatusCallback {
void onAccountStatusReceived(String[] accountStatus);
}
答: 暂无答案
评论