2014-07-14 106 views
0

虽然有几个人提出了关于将片段投射到活动(以及如何工作)的问题,但我无法找到适合我的修复方法。我正在经历this tutorial,并且有一个包含两个片段的主要活动。我可能丢失相当明显的东西:无法从片段投射到片段列表

有错误,我得到:无法从片段转换为FragmentListView(line18)

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

} 
+3

简单的回答:你的'FragmentListView'不是'Fragment'。 –

+0

@ user3739849感谢您的回复,我应该做些什么改变? – user3739849

+0

我仍然卡住。 (部分)FragmentListView的代码如下:'package com.example.numeraid; 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); } ... }' – user3739849

回答

0

感谢@corsair对于这个答案,最初发布的comment

我的猜测是您的FragmentListView类延伸android.support.v4.app.Fragment 而不是android.app.Fragment。如果您想使用支持版本,那么您应该使用getSupportFragmentManager()而不是getFragmentManager()来使用 。否则,您需要 扩展android.app.Fragment无处不在您正在跟随的教程中。

+0

您可以通过单击左侧的复选标记将此答案标记为已接受。 – corsair992