【问题标题】:Android - Nested Fragments [closed]Android - 嵌套片段[关闭]
【发布时间】:2014-11-11 21:15:57
【问题描述】:

我试图有几个选项卡,并且在每个选项卡中我希望有一个列表视图,它将引导我到另一个片段(另一个列表视图)

每当我按下 Android.java 文件中 ListView 上的元素之一时,它就会崩溃。

Android.java

package com.example.tabs;

import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Android extends Fragment {

private ArrayList<String> listOfElements;
private ListView trackView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View android = inflater.inflate(R.layout.android_frag, container, false);
    trackView = (ListView) android.findViewById(R.id.android_list);
    listOfElements = new ArrayList<String>();
    listOfElements.add("Scott");
    listOfElements.add("james");
    listOfElements.add("mike");

    TrackAdapter trackAdapter = new TrackAdapter(getActivity(), listOfElements);
    trackView.setAdapter(trackAdapter);
    trackView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // _mClickListener.onViewSelected(position, listOfTracks);
            try {
                Fragment videoFragment = new NestedAndroidFragment();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(android.getId(), videoFragment);
                transaction.commit();

            } catch (Exception e) {
                Log.d("TabsApp", e.toString());
            }
        }
    });

    return android;
}
}

我是这样开始的:

android_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

    <ListView
        android:id="@+id/android_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

NestedAndroidFragment.java

package com.example.tabs;

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

public class NestedAndroidFragment extends Fragment {
private ListView trackView;
public NestedAndroidFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.nested_frag, container, false);
    trackView = (ListView) root.findViewById(R.id.nested_android_list);
    return root;
}

/*  private void destroyFragment() {
    getChildFragmentManager().beginTransaction().hide(this).commit();
}*/  
}

nested_fragment.xml(我把几乎相同的 android 片段 - 找不到一个很好的例子)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

    <ListView
        android:id="@+id/nested_android_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</FrameLayout>

以前有人做过类似的事情吗?非常感谢您的帮助。

这是堆栈跟踪

11-11 21:35:51.808:E/AndroidRuntime(28384):致命异常:主要 11-11 21:35:51.808: E/AndroidRuntime(28384): android.content.res.Resources$NotFoundException: 找不到资源 ID #0xffffffff 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.content.res.Resources.getResourceName(Resources.java:2639) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.os.Handler.handleCallback(Handler.java:730) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.os.Handler.dispatchMessage(Handler.java:92) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.os.Looper.loop(Looper.java:137) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 android.app.ActivityThread.main(ActivityThread.java:5419) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 java.lang.reflect.Method.invoke(Method.java:525) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 11-11 21:35:51.808: E/AndroidRuntime(28384): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 11-11 21:35:51.808: E/AndroidRuntime(28384): at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 更多代码!更多代码!我们没有比为懒惰的开发者调试更好的事情了!
  • 你能显示错误堆栈跟踪吗?并且请删除与问题无关的代码部分。只有 Android.java 和 android_frag.xml 应该开始。

标签: java android listview android-fragments android-nested-fragment


【解决方案1】:

我明白你做了什么——你试图用 NestedAndroidFragment 的实例替换 android_frag.xml 中的视图内容。您实际上有两个错误 - 崩溃是第一个。我很快就会解释第二个。

您在以下行中尝试执行的操作是错误的。

transaction.replace(android.getId(), videoFragment); // you are trying to get the ID of the entire inflated view
// you should be just using the id of the FrameLayout here

在片段中,您应该用片段替换根级别容器的内容。你想要做的是让片段 B 驻留在片段 A 中,但替换所有 A。你看到这个范式是如何被打破的吗?您不能要求 B 在替换 A 的同时驻留在 A 中。这就是您崩溃的原因。解决这个问题非常简单。在android_frag.xml中,为FrameLayout添加一个id属性如下:

android:id="@+id/container"

现在回到您的 Android.java 并替换您进行替换事务的行:

transaction.replace(R.id.container, videoFragment); // you can directly reference the id here

这将解决您的崩溃问题,但您会注意到另一个问题 - videoFragment 的内容将覆盖您的 ListView 的内容。这样做的原因是,在使用片段时,Android 希望您在需要时填充容器。但是您在 xml 中定义了一个 ListView。一旦在 xml 中定义了一个小部件,您就不能像使用片段一样继续替换或删除它。一旦它在 xml 中,它将始终在该 xml 所在的每个视图中。在您的情况下,最好的设计是让您的 Android 类扩展一个 ListFragment,然后从那里获取东西(但这只是从您现在提出的内容来看。我不确定这将如何与您的其他代码交互,并且因此,如果它对您有用,请仅将我的声明作为建议)。

作为处理你现在拥有的东西的一种解决方法,你需要在用片段替换之前隐藏你的 ListView。因此,将以下行添加到您的 onItemClick() 方法中。

trackView.setVisibility(View.GONE); // put this just before the replace statement
// you can have it after the replace but in theory it would mean that the ListView would show up in the background for a fraction of a moment.
// Might not be noticeable but just good to do it this way

【讨论】:

  • 谢谢,至少我在这方面更进了一步……如果我再次遇到不清楚的问题,我会在此或/上做更多的阅读,并且可能会在此处添加另一个问题。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多