2013-02-14 91 views
0

我有一個應用程序,將有3個片段。它是這樣在xml中定義的。 :如何將一個ViewPager放入一個片段?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id = "@+id/activity_main_large" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" xmlns:tools="http://schemas.android.com/tools"> 

    <fragment 
     android:id="@+id/menu_category_fragment" 
     android:name="com.thesis.menubook.MenuCategory" 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="0.14" 
     layout="@layout/activity_menu_category" /> 

    <fragment 
     android:id="@+id/menu_fragment" 
     android:name="com.thesis.menubook.MenuFragment" 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="0.71" 
     layout="@layout/activity_menu_fragment" /> 


    <fragment 
     android:id="@+id/orderlist_fragment" 
     android:name="com.thesis.menubook.OrderListFragment" 
     android:layout_width="167dp" 
     android:layout_height="match_parent" 
     layout="@layout/activity_order_list" /> 

</LinearLayout> 

我想中間片段是「swipble」,用戶將翻頁。所以com.thesis.menubook.MenuFragment會調用將包含查看傳呼機佈局

這裏是java文件:

package com.thesis.menubook; 

import android.annotation.TargetApi; 
import android.support.v4.app.Fragment; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class MenuFragment extends FragmentActivity { 
    private MyAdapter mAdapter; 
    private ViewPager mPager; 

    static String [] menu_name = {"a", "b","c"}; 
    static String [] menu_description = {"0", "1", "2"}; 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_menu_fragment); 
     mAdapter = new MyAdapter(getSupportFragmentManager()); 

     mPager = (ViewPager) findViewById(R.id.pager); 
     mPager.setAdapter(mAdapter); 
    } 

    public static class MyAdapter extends FragmentPagerAdapter { 
     public MyAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public int getCount() { 
      return 3; 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
      case 0: 
       return new ImageFragment(R.drawable.caesar_salad,menu_name[0], menu_description[0]); 
      case 1: 
       return new ImageFragment(R.drawable.albondigas_pasta,menu_name[1], menu_description[1]); 
      case 2: 
       return new ImageFragment(R.drawable.salmon_entrada,menu_name[2], menu_description[2]); 
      default: 
       return null; 
      } 
     } 
    } 
} 

這是MenuFragment的佈局,其中viewpager定義

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" android:gravity="bottom|top" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp"> 

    <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

</RelativeLayout> 

這是ImageFragment類:

package com.thesis.menubook; 



import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class ImageFragment extends Fragment { 


     private int imageResourceId; 
     private String name; 
     private String namedescription; 



    public ImageFragment(int imageResource, String name2, String description) { 
     this.imageResourceId = imageResource;  
     this.name = name2; 
     this.namedescription = description; 
    } 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.e("Test", "hello"); 
    } 

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

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.layout_menu, container, false); 


     RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.main_layout); 
     layout.setBackgroundResource(imageResourceId); 
     TextView tv = (TextView) view.findViewById(R.id.menu_name); 
     tv.setText(name.toString()); 
     TextView tv2 = (TextView) view.findViewById(R.id.menu_description); 
     tv2.setText(namedescription.toString()); 

     Button button = (Button) view.findViewById(R.id.add); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 


      TextView tx = (TextView) v.findViewById(R.id.menu_name); 
      String mname = tx.toString(); 

      TextView tx1 = (TextView) v.findViewById(R.id.menu_description); 
      String mdesc = tx1.toString(); 

      EditText et = (EditText) v.findViewById(R.id.editText1); 
      String q = et.toString(); 




      } 
     }); 

     return view; 

} 


} 

And th是是ImageFragment的尋呼機

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="top|bottom|center_vertical" 
    android:background="@drawable/caesar_salad" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    android:id="@+id/main_layout" 
    > 

    <TextView 
     android:id="@+id/menu_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/menu_name" 
     android:textSize="40sp" 
     android:background="#2f000000" android:textColor = "@android:color/white"/> 

    <TextView 
     android:id="@+id/menu_description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/menu_name"  
     android:background="#2f000000" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     android:textStyle="italic" 
     android:typeface="sans" 
     android:text ="@string/menu_description" 
     android:layout_margin="10dp"> 

    </TextView> 

    <Button 
     android:id="@+id/add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_toRightOf="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:text="@string/add" 
     android:textColor="@android:color/white"/> 
    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/quantity" android:layout_alignTop="@+id/quantity" android:layout_centerHorizontal="true" android:layout_toRightOf="@+id/quantity" android:layout_marginLeft="5dp" android:background="@android:color/white" android:ems="2" android:inputType="number"> 
     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/quantity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/menu_description" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="20dp" 
     android:background="#2f000000" 
     android:text="@string/quantity" 
     android:textColor="@android:color/white" 
     android:textSize="25sp" 
     android:textStyle="italic" 
     android:typeface="sans"/> 
    <Button 
     android:id="@+id/viewOrdersList" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_toRightOf="@+id/add" 
     android:layout_centerHorizontal="true" 
     android:text="@string/orderlist" 
     android:textColor="@android:color/white"/> 



</RelativeLayout> 
從我的角度,我認爲點

這樣的內容的XML文件,它會正常工作,因爲它具有良好的層次感,我認爲?

但我得到這樣一個錯誤:

02-14 23:22:13.499: E/AndroidRuntime(399): Caused by: java.lang.ClassCastException: com.thesis.menubook.MenuFragment cannot be cast to android.app.Fragment 

你能不能投了Viewpager成片段?

這裏是我的logcat:

02-14 23:22:13.499: E/AndroidRuntime(399): FATAL EXCEPTION: main 
02-14 23:22:13.499: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.os.Looper.loop(Looper.java:132) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.ActivityThread.main(ActivityThread.java:4025) 
02-14 23:22:13.499: E/AndroidRuntime(399): at java.lang.reflect.Method.invokeNative(Native Method) 
02-14 23:22:13.499: E/AndroidRuntime(399): at java.lang.reflect.Method.invoke(Method.java:491) 
02-14 23:22:13.499: E/AndroidRuntime(399): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
02-14 23:22:13.499: E/AndroidRuntime(399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
02-14 23:22:13.499: E/AndroidRuntime(399): at dalvik.system.NativeStart.main(Native Method) 
02-14 23:22:13.499: E/AndroidRuntime(399): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.view.LayoutInflater.inflate(LayoutInflater.java:479) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.view.LayoutInflater.inflate(LayoutInflater.java:391) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.view.LayoutInflater.inflate(LayoutInflater.java:347) 
02-14 23:22:13.499: E/AndroidRuntime(399): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.Activity.setContentView(Activity.java:1780) 
02-14 23:22:13.499: E/AndroidRuntime(399): at com.thesis.menubook.MenuMain.onCreate(MenuMain.java:13) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 
02-14 23:22:13.499: E/AndroidRuntime(399): ... 11 more 
02-14 23:22:13.499: E/AndroidRuntime(399): Caused by: java.lang.ClassCastException: com.thesis.menubook.MenuFragment cannot be cast to android.app.Fragment 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.Fragment.instantiate(Fragment.java:493) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.Fragment.instantiate(Fragment.java:468) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.app.Activity.onCreateView(Activity.java:4132) 
02-14 23:22:13.499: E/AndroidRuntime(399): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664) 
02-14 23:22:13.499: E/AndroidRuntime(399): ... 20 more 
02-14 23:22:13.979: D/dalvikvm(399): GC_CONCURRENT freed 328K, 6% free 7419K/7879K, paused 9ms+41ms 
02-14 23:22:22.750: I/Process(399): Sending signal. PID: 399 SIG: 9 

回答

3

你ViewPager是你ImageFragment的一部分,不是你MenuFragmentFragmentActivity

你必須設置你的ViewPagerImageFragment(與你的吹氣你onCreateView )

將您的ViewPager移動到ImageFragment

private ViewPager mPager; 

並與您的吹氣設置:評論後

mPager = (ViewPager) view.findViewById(R.id.pager); 

編輯:不改變你的XML

  • 你有FragmentActivity其佈局您發佈第一XML(其中3個片段支持)===>因此,ViewPager不在這裏
  • 每個片段類都通過onCreateView來照顧自己的佈局,並將片段返回給顯示===>這是其中ViewPager是
+0

好的,我會嘗試這一個。通過Viewpager你的意思是我在xml中定義的viewpager?或java文件中的Viewpaged? – 2013-02-14 15:38:23

+0

我trasnferred它,但我得到''getSupportFragmentManager()' – 2013-02-14 15:43:18

+0

'紅色線我控制它獲取片段管理器,並嘗試它現在 – 2013-02-14 15:46:40

1
<fragment 
     android:id="@+id/menu_fragment" 
     android:name="com.thesis.menubook.MenuFragment" 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="0.71" 
     layout="@layout/activity_menu_fragment" /> 

com.thesis.menubook.MenuFragment延伸FragmentActivity。這是一個活動,而不是片段。這就是爲什麼你會得到ClassCastException。

+0

所以我只是把它作爲片段? – 2013-02-14 15:35:24

+0

我試過這個,但後來我無法設置我的MenuFragment – 2013-02-14 15:37:07

+0

的contentview我不認爲你的當前代碼有一個簡單的改變。你的問題是ViewPager中的ViewPager。爲此,您需要對Viewpager和Override OnTouchIntercept(...)進行子類化,然後刪除當前的「片段內的片段」並將其更改爲View或片段。在我看來,其中的任何一項都有很大的改變。 – wtsang02 2013-02-14 15:51:19

相關問題