Android 错误:无法从静态上下文引用非静态方法setJavaScriptEnabled(布尔值)
我试图从头开始构建一个web视图应用程序,由于我是新手,所以在第22行设置websetting.setjavascriptenabled(true)时遇到了错误;它没有接受 错误弹出窗口Android 错误:无法从静态上下文引用非静态方法setJavaScriptEnabled(布尔值),android,android-webview,Android,Android Webview,我试图从头开始构建一个web视图应用程序,由于我是新手,所以在第22行设置websetting.setjavascriptenabled(true)时遇到了错误;它没有接受 错误弹出窗口 error: non-static method setJavaScriptEnabled(boolean) cannot be referenced from a static context WebSettings.setJavaScriptEnabled(true); 我的项目设置适用于
error: non-static method setJavaScriptEnabled(boolean) cannot be referenced from a static context
WebSettings.setJavaScriptEnabled(true);
我的项目设置适用于android Oreo 8.1
package net.jptechsolutions.jptechinvoice;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
public WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview=(WebView)findViewById(R.id.webview);
WebSettings webSettings=mywebview.getSettings();
WebSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("https://*********************");
mywebview.setWebChromeClient(new WebChromeClient());
}
@Override
public void onBackPressed() {
if (mywebview.canGoBack())
{
mywebview.goBack();
}
else
{
super.onBackPressed();
}
}
使用
而不是
WebSettings.setJavaScriptEnabled(true);
您正在将类名与方法名一起使用。将您定义的对象名称与方法名称一起使用。使用
而不是
WebSettings.setJavaScriptEnabled(true);
您正在将类名与方法名一起使用。将您定义的对象名与方法名一起使用。您可以调用WebSettings的静态函数。它应该是:
webSettings。setJavaScriptEnabled(true)代码>您可以调用WebSettings的静态函数。它应该是:webSettings。setJavaScriptEnabled(true)
使用webSettings
代替webSettings
webSettings
是类名,而webSettings
是变量名setJavaScriptEnabled
不是静态的,因此您不能从类名中调用它,只能从变量名中调用它使用webSettings
而不是webSettings
是类名,而webSettings
是变量名setJavaScriptEnabled
不是静态的,因此你不能从类名中调用它,只能从变量名中调用。非常感谢你的帮助,如果我想从我的应用程序打开的站点下载文件,我应该添加什么,或者尝试一下。mWebView.setDownloadListener(new DownloadListener(){public void onDownloadStart(字符串url、字符串userAgent、字符串contentDisposition、字符串mimetype、长contentLength){Intent i=new Intent(Intent.ACTION_视图);i.setData(Uri.parse(url));startActivity(i);});感谢你的回复…idk不知怎的我做到了,但现在我面临着一些新的…你能看看这个吗非常感谢你的帮助,如果我想从我的应用程序打开的网站下载文件,我应该添加什么或尝试一下。mWebView.setDownloadListener(new DownloadListener(){public void onDownloadStart(字符串url、字符串userAgent、字符串contentDisposition、字符串mimetype、长contentLength){Intent i=new Intent(Intent.ACTION_视图);i.setData(Uri.parse(url));startActivity(i);});谢谢你的回复…idk不知何故我做到了,但现在我面临着一些新的…你能看看这个吗