gpt4 book ai didi

java - 带有 Fragments 的 AdapterView 不支持 addView(View)

转载 作者:行者123 更新时间:2023-12-01 11:05:01 25 4
gpt4 key购买 nike

我的应用程序中有 2 个 fragment ,我正在努力为平板电脑 UI 制作 2 Pane 模式,并且它运行正常,但单 Pane 模式(对于普通手机)每次运行应用程序时都会崩溃。

这是MainActivity..

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

if (findViewById(R.id.detail_container) != null) {
// The detail container view will be present only in the large-screen layouts
// (res/layout-sw600dp). If this view is present, then the activity should be
// in two-pane mode.
mTwoPane = true;
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.detail_container, new DetailFragment(), DETAILFRAGMENT_TAG)
.commit();
}
}
else{
mTwoPane = false;
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, new PlaceholderFragment())
.commit();
}

我有2个activity_main.xml,其中在文件夹layout-sw600dp中

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_container"
android:name="com.example.moviefinal.MainFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@android:layout/list_content"/>

这适用于 2 Pane 模式(平板电脑 UI)..

<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"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context="com.example.moviefinal.MainActivity">

<!--
This layout is a two-pane layout for the Items master/detail flow.
-->

<fragment
android:id="@+id/main_container"
android:name="com.example.moviefinal.MainFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@android:layout/list_content"
/>
<FrameLayout
android:id="@+id/detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4" />

</LinearLayout>

这是 MainFragment.java

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
db = new DBAdapter(this.getActivity());

gridView = (GridView) rootView.findViewById(R.id.gridview);
ImageAdapter imgAdapter = new ImageAdapter(getActivity());
gridView.setAdapter(imgAdapter);

Fragment_main.xml

    <?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gridview"
android:name="com.example.moviefinal.MainFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="85dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:gravity="center"

android:stretchMode="columnWidth"
tools:context=".MainActivity$PlaceholderFragment"
/>

我遇到了这些错误:

10-09 14:09:15.841  16029-16029/com.example.moviefinal E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.moviefinal, PID: 16029
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moviefinal/com.example.moviefinal.MainActivity}: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:456)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:968)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我认为 GridView 布局导致了这个错误!任何帮助!提前致谢。

最佳答案

您的第一个问题是:

<fragment
android:id="@+id/main_container"
android:name="com.example.moviefinal.MainFragment"

如果您在 XML 中声明 fragment ,则无法以编程方式将其替换为另一个 fragment 。

只需在此处放置一个 FrameLayout 并在 View 创建时添加您的 fragment 即可。

另外,将其从 GridView 元素 XML 中取出:

android:name="com.example.moviefinal.MainFragment"

我认为这就是导致“不支持 addView()”的原因。

关于java - 带有 Fragments 的 AdapterView 不支持 addView(View),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33045297/

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