如果这是个愚蠢的错误,请原谅。我的活动布局(xml)中有两个片段,为了让它们进行通信,我试图在我的主要活动中获取一个片段的实例。我之前已经做过了,但是这次它抛出了erro (所需的视频片段,但是找到了片段),我不知道我做错了什么。可能只是个愚蠢的错误。你能帮忙吗。提前谢谢。
下面的是代码片段:
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发布于 2015-06-10 07:54:52
也许您需要在XML中添加类属性。
<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"/>发布于 2015-06-10 08:59:52
你得知道
getSupportFragmentManager()对片段使用支持库,在这种情况下,应该始终使用支持包类。VideoListFragment扩展android.app.ListFragment; 而不是 android.support.v4.app.ListFragment; (这会导致错误)。并确保片段布局具有带有ListView id的"@android:id/list"。https://stackoverflow.com/questions/30749648
复制相似问题