2017-09-08 177 views
-4

我目前正在编写一个应用程序,将我的网站放入它。我也提出了许可请求,但现在onCreate,setContentViewfindViewById不能得到解决(它们几乎处于编码的末尾)。 这里是我的MainActivity.java:Android Webview乘法方法无法解决

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(); 
      } 
     } 
    } 

回答

0

你复制并粘贴整个文件或做它块?如果这是整个文件,那么在public class GeoWebViewClient extends WebViewClient之后,它看起来像你有一个太多的右括号。所以你实际上已经关闭了活动课。以下类public class GeoWebChromeClient extends WebChromeClient被视为不在嵌入类中,而最后一个功能似乎只是挂在那里而不是任何类的一部分。

这两条连续的线与一个在另一个之上首先要看。了解代码格式的Ctrl + Alt + l快捷键。这应该会让这类问题变得非常清楚。

HTH,迈克

+0

谢谢:)我新的android和java所以我不认识这样的错误如此容易。你为我节省了很多工作 – Sentry