简体   繁体   English

膨胀类片段时出错。 必须指定唯一的android:id,android:tag,或者具有id的父级

[英]Error inflating class fragment. Must specify unique android:id, android:tag, or have a parent with an id

I am trying to run a fragment but my application keeps on crashing when i run it. 我正在尝试运行一个片段,但是当我运行它时,我的应用程序一直在崩溃。 I am trying to load the main activity so that i can display the webview in it but i am having no luck. 我正在尝试加载主要活动,以便我可以在其中显示webview但我没有运气。

MainActivity 主要活动

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Fragment code 片段代码

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class WebViewFragment extends Fragment {

    private WebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        mWebView = (WebView) view.findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("https://www.google.co.uk/");

        return view;
    }
}

Manifest 表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bashirsentongo.webviewapp">
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

activity_main.xml activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bashirsentongo.webviewapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        class="com.example.bashirsentongo.webviewapp.WebViewFragment"/>
</RelativeLayout>

fragment_main.xml fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.main">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>
    />
</FrameLayout>

From the official documentation: 从官方文档:

Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:

Supply the android:id attribute with a unique ID.
Supply the android:tag attribute with a unique string.
If you provide neither of the previous two, the system uses the ID of the container view.

https://developer.android.com/guide/components/fragments.html https://developer.android.com/guide/components/fragments.html

Also you find an example here using an id for the fragment: 你也可以在这里找到一个使用片段id的例子:

<fragment android:name="com.example.news.ArticleListFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

You seem to be confused with how to implement a Fragment. 您似乎对如何实现片段感到困惑。

The Fragment is loaded onto a viewgroup (typically FrameLayout) contained within the Activity, there isn't really a need for the Fragment to have a FrameLayout in your case. Fragment被加载到Activity中包含的一个viewgroup(通常是FrameLayout),在你的情况下,Fragment不需要有一个FrameLayout。 (Because the TextView and Webview will overlap) (因为TextView和Webview会重叠)

That being said, once you have a FrameLayout in place of the use of the <fragment> tag, think about, how would you load the Fragment in the Activity? 话虽这么说,一旦你有一个FrameLayout代替<fragment>标签的使用,想一想,你将如何加载活动中的片段? A FragmentTransaction . FragmentTransaction What does that need? 那需要什么? An id for the view container to load the Fragment. 视图容器的id,用于加载Fragment。 And that's your error - you don't have one. 这是你的错误 - 你没有错误。

All of your <fragment> containers should have unique ID. 您的所有<fragment>容器都应具有唯一ID。 Then just set ID for root RelativeLayout . 然后只需为根RelativeLayout设置ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM