在哪里更改此 TextView 以使用 WebView?

Where do I change this TextView to use a WebView?

好的,我正在研究 Android Boot Camp 中的第 9 章教程,我有...实际上有几个问题。这本书是为 Android Studio 的旧版本编写的,但我的 class 使用的是最新版本。我已尽我所能查找最新版本的教程,但它们已经变得非常罕见了。

第 9 章涵盖了一个 Master/Detail 流程教程,其中一些内容有效,而另一些无效。

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

我尝试简单地将 WebView 转换为与 TextView 匹配,但是 .loadUrl 将不起作用,当我使用 WebView 时,我得到一个 "unexpected cast error. Layout tag was 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 文件并尝试 运行 的程度。

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

AndroidManifest.xml

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

In Your Layout where the webView is

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

打开布局文件item_detail.xml(或者是书里的fragment_item_detail.xml?)将 <TextView> 元素更改为 <WebView>。然后 "Unexpected cast error" 应该消失。