无法从Fragment转换为Fragment ListView

时间:2014-07-14 19:48:52

标签: java android android-intent android-fragments

虽然有几个人提出了有关将片段投射到活动中的问题(以及这不起作用),但我找不到适合我的修复方法。我正在经历this tutorial,并且有一个包含两个片段的主要活动。我可能错过了一些相当明显的东西:

我得到错误:无法从Fragment转换为FragmentListView(第18行)

MainActivity:

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends Activity implements FragmentListView.OnSiteSelectedListener{

Fragmentwebview web;
FragmentListView list;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = getFragmentManager();
    list = (FragmentListView) manager.findFragmentById(R.id.fragment1);
    list.setRefrence(this);
}

@Override
public void onSiteSelected(int i) {
    // TODO Auto-generated method stub

    web = (Fragmentwebview) manager.findFragmentById(R.id.fragment2);
    // Check for landscape mode
    if (web!= null && web.isVisible())
    {
        web.setNewPage(i);
    }
    else
    {
        Intent intent = new Intent(this , FragmentSupport.class);
        intent.putExtra("index", i);
        startActivity(intent);
    }
}

}

清单:          

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.numeraid.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.numeraid.FragmentSupport"
        android:label="@string/title_activity_fragment_support" >
    </activity>
    <activity
        android:name="com.example.numeraid.FragmentListView"
        android:label="@string/title_activity_fragment_list_view" >
    </activity>
    <activity
        android:name="com.example.numeraid.Fragmentwebview"
        android:label="@string/title_activity_fragmentwebview" >
    </activity>
</application>

</manifest>

导致错误的第一个片段

<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent"     android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<ListView 
    android:name="com.example.NumerAid.list"
    android:id="@+id/list" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent">
</ListView>

</LinearLayout>

编辑:添加FragmentListView类

   package com.example.numeraid;

   import android.R.string;
   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.AdapterView;
   import android.widget.ArrayAdapter;
   import android.widget.ListView;

   public class FragmentListView extends Fragment implements    AdapterView.OnItemClickListener
   {
   ListView list;
   String [] websites = {"Google","Facebook","Twitter","Xda-developer"};


   OnSiteSelectedListener SiteListener;

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

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
   View v = inflater.inflate(R.layout.fragmentlistview, container, false);
   list = (ListView) v.findViewById(R.id.list);
   list.setAdapter(new ArrayAdapter<string>(getActivity().getApplicationContext(),     android.R.layout.simple_list_item_1));
    list.setOnItemClickListener(this);
    return v;
    }

@Override
public void onItemClick(AdapterView adapterView, View view, int position, long l)
{
    SiteListener.onSiteSelected(position);
}

// Container Activity must implement this interface
public interface OnSiteSelectedListener {
    public void onSiteSelected(int i);
}

public void setRefrence(OnSiteSelectedListener siteListener)
{
    this.SiteListener = siteListener;
}

}

1 个答案:

答案 0 :(得分:0)

感谢@corsair提供此答案,最初发布为comment

  

我猜您的FragmentListView课程延长了android.support.v4.app.Fragment   而不是android.app.Fragment。如果您想使用支持版本,那么您应该   使用getSupportFragmentManager()代替getFragmentManager()。否则,你需要   如您所关注的教程中所做的那样,将android.app.Fragment扩展到任何地方。