【问题标题】:Where do I change this TextView to use a WebView?在哪里更改此 TextView 以使用 WebView?
【发布时间】:2019-04-15 21:48:15
【问题描述】:

好的,我正在编写 Android Boot Camp 的第 9 章教程,但我遇到了……实际上有一些问题。这本书是为旧版本的 Android Studio 编写的,但我的班级使用的是最新版本。我已尽我所能查找最新版本的教程,但它们变得非常罕见。

第 9 章介绍了一个主/详细流程教程,其中一些已经奏效,而另一些则没有。

我现在的立场是 TextView/WebView 问题。

我尝试简单地将 WebView 转换为与 TextView 匹配,但 .loadUrl 不起作用,当我使用 WebView 时,我收到“意外的转换错误。布局标签是 TextView”。而且 Android Studio 不会告诉我布局标签的声明位置,所以我目前正在逐行梳理所有文件。我不确定此源布局标记是否在 .xml、.java 中,或者我是否应该查看清单。

我相信这意味着我必须将源布局更改为 WebView,尽管我在章节本身中找不到任何关于它的内容,除非确保我有正确的导入。

package com.example.bikeandbarge;

import android.app.Activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.example.bikeandbarge.dummy.DummyContent;

public class ItemDetailFragment extends Fragment {

public static final String ARG_ITEM_ID = "item_id";


private DummyContent.DummyItem mItem;


public ItemDetailFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments().containsKey(ARG_ITEM_ID)) {
        // Load the dummy content specified by the fragment
        // arguments. In a real-world scenario, use a Loader
        // to load content from a content provider.
        mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));

        Activity activity = this.getActivity();
        CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
        if (appBarLayout != null) {
            appBarLayout.setTitle(mItem.content);
        }
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.item_detail, container, false);

    // Show the dummy content as text in a TextView.
    if (mItem.id.equals("1")) {
        rootView = inflater.inflate(R.layout.photos, container, false);
    }
    if (mItem.id.equals("2")) {
        rootView = inflater.inflate(R.layout.tour, container, false);
    }
    // Can not get this to update to WebView-not certain where the layout tab is textView
    //if (mItem.id.equals("3")) {
    //            ((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url ); }
    //Can replace WebView with TextView but won't recognize .loadUrl without WebView
    if (mItem.id.equals("3")) {
        ((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url );

    }

    return rootView;
}
}

我希望它能够在 loadUrl 实际工作的情况下运行。

WebView 和 TextView 都不允许我运行程序。根本无法为我编译 apk 文件,甚至无法对其进行测试。我希望至少可以编译 apk 文件并尝试运行它。

【问题讨论】:

    标签: java android url webview textview


    【解决方案1】:

    打开布局文件item_detail.xml(还是书上的fragment_item_detail.xml?),将<TextView>-元素改为<WebView>。那么“Unexpected cast error”应该会消失。

    【讨论】:

    • 谢谢!事实证明,旧版和新版 Android Studio 之间的命名约定比我最初想象的要多得多。找到它在 item_detail.xml 中自动声明 textView 的位置,将其更改为 webview,现在一切正常。
    【解决方案2】:

    在您的 webView 和清单中添加以下代码:

    AndroidManifest.xml

    <application
            ....
            android:usesCleartextTraffic="true"
            android:hardwareAccelerated="true"
            .....            
     >
    

    在 WebView 所在的布局中

    <WebView
        ....
        android:usesCleartextTraffic="true"
        ....        
      ></WebView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 2016-09-24
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多