3

我的问题是我运行 android 应用程序并显示下面给出的错误日志。如何解决它。

public class SeoShopTest extends Activity {

    WebView myWebView;
    TextView mDisplay;
    AsyncTask<Void, Void, Void> mRegisterTask;
    String Message;
    String deviceId;
    String regId;
    IntentFilter gcmFilter;
    SharedPreferences sharedPref;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        checkNotNull(SERVER_URL, "https://android.googleapis.com/gcm/send");
        checkNotNull(SENDER_ID, "201519466560");

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        setContentView(R.layout.activity_seo_shop_test);

        myWebView = (WebView) findViewById(R.id.web);

        gcmFilter = new IntentFilter();
        gcmFilter.addAction("GCM_RECEIVED_ACTION");

        sharedPref = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        String CheckTokenId = sharedPref.getString(
                getResources().getString(R.string.token_id), "");
        String CheckShopId = sharedPref.getString("shop_id", "");

        Log.v("log", " check TokenID" + CheckTokenId + "  check Shop id "
                + CheckShopId);

        if ((!CheckTokenId.equals("")) && (!CheckShopId.equals(""))) {
            Log.v("log",
                    "  lhl hklkh lkh ll  : : :  shopId and TokenId Get IT ");
            Intent i = new Intent(SeoShopTest.this, devicephp.class);
            i.putExtra("tokenId", CheckTokenId);
            i.putExtra("shop_id", CheckShopId);
            i.putExtra("app_var", "1");
            startActivity(i);
        }


        final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(
                this);
        myWebView.addJavascriptInterface(myJavaScriptInterface,
                "AndroidFunction");

        // mDisplay = (TextView) findViewById(R.id.display);
        registerReceiver(mHandleMessageReceiver, new IntentFilter(
                DISPLAY_MESSAGE_ACTION));
        regId = GCMRegistrar.getRegistrationId(SeoShopTest.this);

        WebSettings settings = myWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadWithOverviewMode(true);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        //settings.setAllowUniversalAccessFromFileURLs(true);

         myWebView.setWebViewClient(new WebViewClient() {  
               public boolean shouldOverrideUrlLoading(WebView view, String url)  {                  

                            //handle stuff here 
                            //e.g. view.loadUrl(url);   
                   Log.v("log"," on ovverRide " + url);
                            return true;    
                        }

                        public void onPageFinished(WebView view, String url) {
                            //dismiss the indeterminate progress dialog
                            Log.v("log", "onPageFinished: " + url);
                            myWebView.setEnabled(false);

                        } 
                    });

        myWebView.loadUrl("file:///android_asset/www/index.html");

        if (regId.equals("")) {
            // Automatically registers application on startup.
            Log.v("log", " sender ID" + SENDER_ID);
            GCMRegistrar.register(this, SENDER_ID);

            regId = GCMRegistrar.getRegistrationId(SeoShopTest.this);

            Log.v("log", " re register main if " + regId);
        } else {
            Log.v("log", " re register main else " + regId);

            // Device is already registered on GCM, needs to check if it is
            // registered on our server as well.
            if (GCMRegistrar.isRegisteredOnServer(this)) {
                // Skips registration.
                // mDisplay.append("already_registered" + "\n");
            } else {
                // Try to register again, but not in the UI thread.
                // It's also necessary to cancel the thread onDestroy(),
                // hence the use of AsyncTask instead of a raw thread.
                final Context context = this;
                mRegisterTask = new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        boolean registered = ServerUtilities.register(context,
                                regId);

                        if (!registered) {
                            GCMRegistrar.unregister(context);
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        mRegisterTask = null;
                    }

                };
                mRegisterTask.execute(null, null, null);
            }
        }

    }

    private void SavePreferences(String key, String value) {
        sharedPref = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }


        public void showToast(String shopId) {

            sharedPref = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            String token_new_ID = sharedPref.getString(getResources()
                    .getString(R.string.token_id), "");

            Log.v("log", "tokenNEWID " + token_new_ID + " shoipdi  " + shopId);

            if ((!shopId.equals("")) && (token_new_ID.equals(""))) {

                SavePreferences("shop_id", shopId);

                Intent i = new Intent(SeoShopTest.this, LoginCompleteUrl.class);
                i.putExtra("shopId", shopId);
                startActivity(i);
            } else if ((!token_new_ID.equals("")) && (!shopId.equals(""))) {

                Log.v("log", "shop id else if " + shopId);
                SavePreferences("shop_id", shopId);
                Intent i = new Intent(SeoShopTest.this, devicephp.class);
                i.putExtra("tokenId", token_new_ID);
                i.putExtra("shop_id", shopId);
                startActivity(i);
            } else {
                Log.v("log", " else token  " + token_new_ID);

            }

        }

        public void MainUrl(String url) {
            Log.v("log"," login main url " + url);

            String url_new = "http://"+url;
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url_new));
            startActivity(i);

        }


    }

    @Override
    protected void onDestroy() {
        if (mRegisterTask != null) {
            mRegisterTask.cancel(true);
        }
        unregisterReceiver(mHandleMessageReceiver);
        GCMRegistrar.onDestroy(this);
        super.onDestroy();
    }

    private void checkNotNull(Object reference, String name) {
        if (reference == null) {
            throw new NullPointerException(
                    "Please set the %1$s constant and recompile the app.");
        }
    }

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // String newMessage = intent.getExtras().getString("message");
            String GcmMessage = intent.getExtras().getString("message");
            int GcmId = intent.getExtras().getInt("id");
            String GCmType = intent.getExtras().getString("type");

            Log.v("log", " new Message from server GsmMessage " + GcmMessage
                    + " id" + GcmId + "type " + GCmType);

            // mDisplay.append(newMessage + "\n");
        }
    };

}

我的错误如下:

02-12 10:37:14.629: ERROR/AndroidRuntime(330): FATAL EXCEPTION: main
    02-12 10:37:14.629: ERROR/AndroidRuntime(330): java.lang.NoSuchMethodError: android.webkit.WebSettings.setAllowUniversalAccessFromFileURLs
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at com.example.seoshoptest.SeoShopTest.onCreate(SeoShopTest.java:93)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.os.Looper.loop(Looper.java:123)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at java.lang.reflect.Method.invoke(Method.java:521)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    02-12 10:37:14.629: ERROR/AndroidRuntime(330):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

9

它崩溃的原因是您可能没有在 API 级别 16 或更高的设备上运行。

从官方网站这里你可以看到方法getAllowUniversalAccessFromFileURLs是在 API 级别 16 之后添加的。所以你会

公共抽象布尔 getAllowUniversalAccessFromFileURLs ()

在 API 级别 16 中添加 获取在文件方案 URL 的上下文中运行的 JavaScript 是否可以访问来自任何来源的内容。这包括从其他文件方案 URL 访问内容。

返回在文件方案 URL 的上下文中运行的 JavaScript 是否可以访问来自任何来源的内容 另请参见 setAllowUniversalAccessFromFileURLs(boolean)

所以要访问这个你应该写下面的代码:

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
于 2013-02-12T05:31:51.717 回答