如何修复此错误使android.webkit.WebView.setBackgroundColor(int)“在空值上无效

wljmcqd8  于 2022-12-31  发布在  Android
关注(0)|答案(1)|浏览(110)

给出了后台网页视图。运行时给出了如何修复此错误的错误。recipe_description = findViewById(R.id.recipe_description); recipe_description此网页视图ID没有数据进入网页视图如何修复此错误字符串htmlText =“file:///android_asset/aboutcertified. html”;此数据html无法加载原因

public class ActivityRecipeDaily extends AppCompatActivity {
private WebView recipe_description;
SharedPref sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Tools.getTheme(this);
    setContentView(R.layout.activity_recipe_detail2);
    Tools.getLayoutDirections(this, RTL_MODE);
    recipe_description.setBackgroundColor(Color.TRANSPARENT);
    recipe_description.setFocusableInTouchMode(false);
    recipe_description.setFocusable(false);
    recipe_description.getSettings().setDefaultTextEncodingName("UTF-8");

    recipe_description = findViewById(R.id.recipe_description);

    WebSettings webSettings = recipe_description.getSettings();
    Resources res = getResources();
    int fontSize = res.getInteger(R.integer.font_size);
    webSettings.setDefaultFontSize(fontSize);

    String mimeType = "text/html; charset=UTF-8";
    String encoding = "utf-8";
    String htmlText = "file:///android_asset/aboutcertified.html";
    String bg_paragraph;
    if (sharedPref.getIsDarkTheme()) {
        bg_paragraph = "<style type=\"text/css\">body{color: #eeeeee;}";
    } else {
        bg_paragraph = "<style type=\"text/css\">body{color: #000000;}";
    }

    String font_style_default = "<style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/font/custom_font.ttf\")}body {font-family: MyFont; font-size: medium; text-align: left;}</style>";

    String text = "<html><head>"
            + font_style_default
            + "<style>img{max-width:100%;height:auto;} figure{max-width:100%;height:auto;} iframe{width:100%;}</style> "
            + bg_paragraph
            + "</style></head>"
            + "<body>"
            + htmlText
            + "</body></html>";

    String text_rtl = "<html dir='rtl'><head>"
            + font_style_default
            + "<style>img{max-width:100%;height:auto;} figure{max-width:100%;height:auto;} iframe{width:100%;}</style> "
            + bg_paragraph
            + "</style></head>"
            + "<body>"
            + htmlText
            + "</body></html>";

    if (RTL_MODE) {
        recipe_description.loadDataWithBaseURL(null, text_rtl, mimeType, encoding, null);
    } else {
        recipe_description.loadDataWithBaseURL(null, text, mimeType, encoding, null);
    }

}
hts6caw3

hts6caw31#

你好@ jayramanjay,我已经添加了评论来修复你的错误。看看吧

public class ActivityRecipeDaily extends AppCompatActivity {
    private WebView recipe_description;
    SharedPref sharedPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.getTheme(this);
        setContentView(R.layout.activity_recipe_detail2);
        Tools.getLayoutDirections(this, RTL_MODE);

        recipe_description = findViewById(R.id.recipe_description); // You need to initialize your webview before accessing its methods otherwise it will give you NullPointerException.
        recipe_description.setBackgroundColor(Color.TRANSPARENT);
        recipe_description.setFocusableInTouchMode(false);
        recipe_description.setFocusable(false);
        recipe_description.getSettings().setDefaultTextEncodingName("UTF-8");

        WebSettings webSettings = recipe_description.getSettings();
        Resources res = getResources();
        int fontSize = res.getInteger(R.integer.font_size);
        webSettings.setDefaultFontSize(fontSize);

        String mimeType = "text/html";
        String encoding = "utf-8";
        String htmlText = "file:///android_asset/aboutcertified.html";
        String bg_paragraph;
        if (sharedPref.getIsDarkTheme()) {
            bg_paragraph = "<style type=\"text/css\">body{color: #eeeeee;}";
        } else {
            bg_paragraph = "<style type=\"text/css\">body{color: #000000;}";
        }

        String font_style_default = "<style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/font/custom_font.ttf\")}body {font-family: MyFont; font-size: medium; text-align: left;}</style>";

        String text = "<html><head>" +
            font_style_default +
            "<style>img{max-width:100%;height:auto;} figure{max-width:100%;height:auto;} iframe{width:100%;}</style> " +
            bg_paragraph +
            "</style></head>" +
            "<body>" +
            htmlText +
            "</body></html>";

        String text_rtl = "<html dir='rtl'><head>" +
            font_style_default +
            "<style>img{max-width:100%;height:auto;} figure{max-width:100%;height:auto;} iframe{width:100%;}</style> " +
            bg_paragraph +
            "</style></head>" +
            "<body>" +
            htmlText +
            "</body></html>";
       
        // Update this condition into your code.

        if (RTL_MODE) {
            recipe_description.loadData(text_rtl, mimeType , encoding);
        } else {
            recipe_description.loadData(text, mimeType , encoding);
        }

    }

你在用
webview.loadDataWithBaseURL(空,文本,MIME类型,编码,空)
方法,并在该方法所需的baseUrl参数的位置传递null,以便它不会向webview加载任何内容。
希望你能在这之后得到解决。让我知道如果你有任何其他的问题。
快乐编码!!

相关问题