【问题标题】:Error inflating class fragment. Must specify unique android:id, android:tag, or have a parent with an id膨胀类片段时出错。必须指定唯一的 android:id、android:tag 或有一个具有 id 的父级
【发布时间】:2016-07-19 13:03:23
【问题描述】:

我正在尝试运行一个片段,但我的应用程序在我运行它时不断崩溃。我正在尝试加载主要活动,以便可以在其中显示 webview,但我没有运气。

主活动

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);
    }
}

片段代码

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;
    }
}

清单

<?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

<?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

<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>

【问题讨论】:

  • XML中的片段标签需要一个ID

标签: android android-fragments fragment


【解决方案1】:

来自官方文档:

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

您还可以在此处找到使用片段 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" />

【讨论】:

    【解决方案2】:

    您似乎对如何实现 Fragment 感到困惑。

    Fragment 被加载到 Activity 中包含的视图组(通常是 FrameLayout)上,在您的情况下,Fragment 并不需要 FrameLayout。 (因为TextView和Webview会重叠)

    话虽如此,一旦你有一个FrameLayout 来代替&lt;fragment&gt; 标签的使用,想想,你将如何在Activity 中加载Fragment?一个FragmentTransaction。那需要什么?用于加载 Fragment 的视图容器的 id。那是你的错误——你没有。

    【讨论】:

    • @Mrnerd 如果这解决了您的问题,请使用答案旁边的复选标记将其标记为已接受
    【解决方案3】:

    您所有的&lt;fragment&gt; 容器都应该有唯一的ID。然后只需为 root RelativeLayout 设置 ID。

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 2011-09-19
      • 2013-05-28
      • 1970-01-01
      相关资源
      最近更新 更多