错误:找不到符号方法findViewById(int) [英] Error: cannot find symbol method findViewById(int)

查看:405
本文介绍了错误:找不到符号方法findViewById(int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小型应用程序中的WebViews遇到问题.我有相关的Web视图,其布局中包含正确的信息,我有直接来自Android开发者网站的示例代码,这些代码对我不起作用.

I'm experiencing an issue with WebViews within my small app. I have the relevant webview with the correct information in my layouts and I have sample code directly from the Android Developer Site that doesn't work for me.

该错误与以下内容有关: findViewById(R.id.webview)方法.

The error is around the: findViewById(R.id.webview)method.

带有事件监听器的类:

package com.bignerdranch.android.transitionexample;

import android.app.Fragment;
import android.os.Bundle;
import android.transition.Scene;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import com.bignerdranch.android.transitionexample.R;

/**
 * A placeholder fragment containing a simple view.
 */
public class TransitionFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_transition_scene_1, container, false);
        final Scene scene = Scene.getSceneForLayout(container, R.layout.fragment_transition_scene_2, getActivity());
        Button goButton = (Button)rootView.findViewById(R.id.goButton);
        goButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransitionManager.go(scene);
                WebView myWebView = (WebView) findViewById(R.id.webview);
                myWebView.loadUrl("http://www.example.com");
                Log.d("","Loading Finished");
            }
        });
        return rootView;
    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scene"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.bignerdranch.android.transitionexample.TransitionFragment">

    <TextView
        android:id="@+id/textView"
        android:text="@string/hello_world"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="@string/hello_world"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="@android:dimen/thumbnail_height"
        android:layout_weight="1.43" />

</LinearLayout>

推荐答案

 WebView myWebView = (WebView) rootView.findViewById(R.id.webview);

将您的onCreateView方法更改为:

private WebView webView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_transition_scene_1, container, false);
    final Scene scene = Scene.getSceneForLayout(container, R.layout.fragment_transition_scene_2, getActivity());
    Button goButton = (Button)rootView.findViewById(R.id.goButton);
    goButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TransitionManager.go(scene);
            webView myWebView = (WebView) rootView.findViewById(R.id.webview);

            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.setWebViewClient(new WebViewClient(){

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    progDailog.show();
                    view.loadUrl(url);

                    return true;
                }
                @Override
                public void onPageFinished(WebView view, final String url) {
                    progDailog.dismiss();
                }
            });

            webView.loadUrl("http://www.example.com");

            Log.d("","Loading Finished");
        }
    });
    return rootView;
}

这篇关于错误:找不到符号方法findViewById(int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆