Java Android Webview乘法方法可以';无法解决
我目前正在编写一个应用程序,将我的网站带入其中。我还请求了权限,但现在无法解决Java Android Webview乘法方法可以';无法解决,java,android,webview,Java,Android,Webview,我目前正在编写一个应用程序,将我的网站带入其中。我还请求了权限,但现在无法解决onCreate、setContentView和findViewById的问题(它们几乎都在编码的末尾)。 以下是我的MainActivity.java: package org.youtivity.youtivity; import android.os.Bundle; import android.Manifest; import android.app.Activity; import android.cont
onCreate
、setContentView
和findViewById
的问题(它们几乎都在编码的末尾)。
以下是我的MainActivity.java:
package org.youtivity.youtivity;
import android.os.Bundle;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static android.content.ContentValues.TAG;
public class MainActivity extends Activity {
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions()) {
// carry on the normal flow, as the case of permissions granted.
}
}
private boolean checkAndRequestPermissions() {
int permissionSendMessage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d(TAG, "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "sms & location services permission granted");
// process the normal flow
//else any one or both the permissions are not granted
} else {
Log.d(TAG, "Some permissions are not granted ask again ");
//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
// shouldShowRequestPermissionRationale will return true
//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
showDialogOK("SMS and Location Services Permission required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
.show();
// //proceed with logic by disabling the related features or quit the app.
}
}
}
}
}
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
/**
* WebViewClient subclass loads all hyperlinks in the existing WebView
*/
public class GeoWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
// view.loadUrl(url);
// return true;
String url2="https://www.youtivity.org/";
// all links with in ur site will be open inside the webview
//links that start ur domain example(http://www.example.com/)
if (url != null && url.startsWith(url2)){
return false;
}
// all links that points outside the site will be open in a normal android browser
else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
}
}
/**
* WebChromeClient subclass handles UI-related calls
* Note: think chrome as in decoration, not the Chrome browser
*/
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}
WebView mWebView;
/** Called when the activity is first created. */
@Override //<------Error HERE (does not overwrite)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //<------Error HERE (onCreate)
setContentView(R.layout.activity_main); //<------Error HERE (setContentView)
mWebView = (WebView) findViewById(R.id.webView);//<------Error HERE (findViewById)
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new GeoWebViewClient());//<------Error HERE (GeoWebViewClient)
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/noinet.html");
}
});
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://www.youtivity.org");
}
@Override
public void onBackPressed() {
// Pop the browser back stack or exit the activity
if (mWebView.canGoBack()) {
mWebView.goBack();
}
else {
super.onBackPressed();
}
}
}
package org.youtivity.youtivity;
导入android.os.Bundle;
导入android.Manifest;
导入android.app.Activity;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.net.Uri;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AlertDialog;
导入android.util.Log;
导入android.webkit.GeolocationPermissions;
导入android.webkit.WebChromeClient;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
导入android.support.v4.app.ActivityCompat;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入静态android.content.ContentValues.TAG;
公共类MainActivity扩展了活动{
公共静态最终整数请求\u ID\u多个\u权限=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
如果(检查并请求权限()){
//在授予权限的情况下,执行正常流程。
}
}
私有布尔校验和请求权限(){
int permissionSendMessage=ContextCompat.checkSelfPermission(此,
清单。权限。读取(外部存储);
int locationPermission=ContextCompat.checkSelfPermission(这是Manifest.permission.ACCESS\u FINE\u位置);
List ListPermissionsNeed=new ArrayList();
if(locationPermission!=已授予PackageManager.PERMISSION){
listPermissionsNeed.add(Manifest.permission.ACCESS\u FINE\u位置);
}
if(permissionSendMessage!=PackageManager.PERMISSION\u已授予){
listPermissionsNeed.add(Manifest.permission.READ\u外部存储);
}
如果(!ListPermissionsNeed.isEmpty()){
ActivityCompat.requestPermissions(此,ListPermissionsNeed.toArray(新字符串[ListPermissionsNeed.size()),请求\u ID\u多个\u权限);
返回false;
}
返回true;
}
公共无效onRequestPermissionsResult(int requestCode,
字符串权限[],int[]grantResults){
Log.d(标记“权限回调调用----------------”);
开关(请求代码){
案例请求\u ID\u多个\u权限:{
Map perms=new HashMap();
//使用这两个权限初始化映射
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.permission_grated);
perms.put(Manifest.permission.ACCESS\u FINE\u位置,PackageManager.permission\u已授予);
//填写用户的实际结果
如果(grantResults.length>0){
for(int i=0;i