【问题标题】:getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);
【发布时间】:2015-06-10 07:20:06
【问题描述】:

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

下面是sn-p代码:

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

}

下面是布局:

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

下面是片段类代码

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

下面是堆栈跟踪

Error:(22, 91) error: inconvertible types
required: VideoListFragment
found:    Fragment

【问题讨论】:

  • 能否展示片段类代码?
  • 能否提供引发异常的堆栈跟踪?
  • 堆栈跟踪以及导致异常的哪一行(我猜我知道是哪一行)会有所帮助!
  • 导致问题的行是 ************* videoListFragment =(VideoListFragment) getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment); *********** 在我持有碎片的活动中

标签: android android-fragments fragmentmanager


【解决方案1】:

也许你需要在 XML 中添加 class 属性。

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

【讨论】:

    【解决方案2】:

    你需要知道

    1. 您正在使用 Fragment 的支持库 getSupportFragmentManager(),在这种情况下,您应该始终使用支持包类。
    2. VideoListFragment 扩展 android.app.ListFragment; 而不是 android.support.v4.app.ListFragment; (这个东西会导致错误)。并确保片段布局具有ListView"@android:id/list" id。

    【讨论】:

    • 在上面试过......我得到 NullPointerException 。我尝试了 getSupportFragmentManager().findViewById(R.id.fragment_id).getClass() 并得到以下错误尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()'跨度>
    • 为什么你要使用getClass(),这就够了videoListFragment =(VideoListFragment) getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment); 如果你没有那个ID的片段,你会得到那个错误。
    • getClass() 我只是用来检查我收到的类对象。我得到了 nullPointer Excpetion。
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多