gpt4 book ai didi

android - 膨胀 fragment 时出错

转载 作者:行者123 更新时间:2023-11-29 14:28:24 31 4
gpt4 key购买 nike

我只是想按照 android dev 上的示例代码来膨胀 Activity 中的 fragment 。我有一个更复杂的项目正在工作,但我遇到了与这里的简单项目相同的错误。谁能指出我正确的方向?

扩展 fragment 的类

package com.example.fragtest;


import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragOne extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_main, container,false);
}

}

扩展FragmentActivity的类

package com.example.fragtest;

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

public class MainActivity extends FragmentActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}



}

fragment 所在的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >

<fragment android:name="com.example.fragtest.FragOne"
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

设置 Activity 名称的 list

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fragtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />

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

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

</manifest>

这是 logcat 输出

07-17 23:45:09.053: E/AndroidRuntime(560): FATAL EXCEPTION: main
07-17 23:45:09.053: E/AndroidRuntime(560): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragtest/com.example.fragtest.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.os.Looper.loop(Looper.java:137)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-17 23:45:09.053: E/AndroidRuntime(560): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 23:45:09.053: E/AndroidRuntime(560): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 23:45:09.053: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 23:45:09.053: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 23:45:09.053: E/AndroidRuntime(560): at dalvik.system.NativeStart.main(Native Method)
07-17 23:45:09.053: E/AndroidRuntime(560): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 23:45:09.053: E/AndroidRuntime(560): at com.example.fragtest.FragOne.onCreateView(FragOne.java:18)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 23:45:09.053: E/AndroidRuntime(560): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.Activity.setContentView(Activity.java:1835)
07-17 23:45:09.053: E/AndroidRuntime(560): at com.example.fragtest.MainActivity.onCreate(MainActivity.java:12)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.Activity.performCreate(Activity.java:4465)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-17 23:45:09.053: E/AndroidRuntime(560): ... 11 more
07-17 23:45:09.053: E/AndroidRuntime(560): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragtest.FragOne
07-17 23:45:09.053: E/AndroidRuntime(560): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
07-17 23:45:09.053: E/AndroidRuntime(560): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 23:45:09.053: E/AndroidRuntime(560): ... 30 more

最佳答案

在扩展 FragmentActivity 的类中,您将 View 设置为名为 activity_main 的 XML 文件。在 activity_main 中,您有一个对您的 Fragment 代码 com.example.fragtest.FragOne 的引用。加载该 XML 时,将执行与 com.example.fragtest.FragOne 关联的代码。 Fragment 的代码返回一个带有 R.id.activity_mainView

该 XML 文件引用了刚刚实例化它的同一 Fragment 代码。你看到了这个难题。

要解决您的问题,请在您的 Fragment 类中创建一个新的 XML 文件(未命名为 activity_main)。不是膨胀 activity_main 而是膨胀一个单独的 XML(不包含对调用它的代码的引用)。实际上 activity_main 应该是容器,这个新布局(例如 fragment_layout)将是 UI 部分。

我喜欢这样想:尽管有教条,Fragment 还是包含三个部分。容器 Activity(这可以,并且在您的情况下应该通过 setContentView 扩展布局 XML 文件),Fragment 的 java 代码(这可以,并且在您的情况下应该,膨胀一个布局....activity_main 以外的东西),以及那个 Fragment 的相关 XML 文件(被你的 Fragment 类膨胀的那个,不要'不包含对其代码的循环引用)。

当然,这些咒语有漏洞,但你明白了吗?

关于android - 膨胀 fragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532361/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com