首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);

getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);
EN

Stack Overflow用户
提问于 2015-06-10 07:20:06
回答 2查看 243关注 0票数 0

如果这是个愚蠢的错误,请原谅。我的活动布局(xml)中有两个片段,为了让它们进行通信,我试图在我的主要活动中获取一个片段的实例。我之前已经做过了,但是这次它抛出了erro (所需的视频片段,但是找到了片段),我不知道我做错了什么。可能只是个愚蠢的错误。你能帮忙吗。提前谢谢。

下面的是代码片段:

代码语言:javascript
运行
复制
public class VideoListingActivity extends ActionBarActivity implements FilterFragment.OnFilterItemSelectedListener{

 public VideoListFragment videoListFragment;


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

    videoListFragment =(VideoListFragment) getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);

}

下面是布局:

代码语言:javascript
运行
复制
<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"
    tools:context="my activity context">


    <fragment
        android:layout_width="match_parent"
        android:layout_height="@dimen/filter_fragment_height"
        android:name="com.text1.text2.text3.pkg.FilterFragment"
        android:id="@+id/frg_video_listing_filter_fragment"
        ></fragment>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.text1.text2.text3.pkg.VideoListFragment"
        android:id="@+id/frg_video_listing_video_listing_fragment"
        ></fragment>

</RelativeLayout>

下面是片段类代码

代码语言:javascript
运行
复制
package com.rrdtech.vidyavaan.android.Fragments;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rrdtech.vidyavaan.android.R;


public class VideoListFragment extends ListFragment {
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
    return     inflater.inflate(R.layout.video_list_fragment,container,false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onStart() {
    super.onStart();
}
}

下面是堆栈跟踪

代码语言:javascript
运行
复制
Error:(22, 91) error: inconvertible types
required: VideoListFragment
found:    Fragment
EN

回答 2

Stack Overflow用户

发布于 2015-06-10 07:54:52

也许您需要在XML中添加类属性。

代码语言:javascript
运行
复制
<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.text1.text2.text3.pkg.VideoListFragment"
    class="com.text1.text2.text3.pkg.VideoListFragment"
    android:id="@+id/frg_video_listing_video_listing_fragment"/>
票数 0
EN

Stack Overflow用户

发布于 2015-06-10 08:59:52

你得知道

  1. 使用getSupportFragmentManager()对片段使用支持库,在这种情况下,应该始终使用支持包类。
  2. VideoListFragment扩展android.app.ListFragment; 而不是 android.support.v4.app.ListFragment; (这会导致错误)。并确保片段布局具有带有ListView id的"@android:id/list"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30749648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档