2014-02-05 87 views
0

改變一個片段予有四個片段(在兩個包)及其相應的4 XML文件它們。三根據tabselect。一是fragement我已經組改變(ComapreFrag.java )有ImagineView button。當我按下它時,我想開始新的片段(* CreditCards.java *)。它怎麼做it.please幫助me.please找到下面我使用的代碼。通過按下按鈕的ImageView

MainActivity.java

import android.support.v4.app.Fragment; 

package com.example.swipetabs; 

import com.tabs.*; 

import android.os.Bundle; 
import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.ActionBar.TabListener; 
import android.app.Activity; 
import android.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.Menu; 
import android.widget.ImageView; 
import com.comparenow.*; 
import android.support.v4.app.FragmentManager; 

public class MainActivity extends FragmentActivity implements TabListener { 

    ActionBar action_bar; 
    ViewPager viewPager; 
    ImageView creditCards; 

    CreditCards credit_cards; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // 
     credit_cards =new CreditCards(); 

     Bundle args=new Bundle(); 

     viewPager=(ViewPager)findViewById(R.id.pager); 
     //intialise fragement 


     viewPager.setAdapter(new MyAdoptor(getSupportFragmentManager())); 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int arg0) { 
       // TODO Auto-generated method stub 
       action_bar.setSelectedNavigationItem(arg0); 

      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 

     action_bar=getActionBar(); 
     //action_bar.setBackgroundDrawable(d) 
     action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     ActionBar.Tab tab1=action_bar.newTab(); 
     tab1.setText("Login"); 

     tab1.setTabListener(this); 

     ActionBar.Tab tab2=action_bar.newTab(); 
     tab2.setText("Compare Now"); 
     tab2.setTabListener(this); 

     ActionBar.Tab tab3=action_bar.newTab(); 
     tab3.setText("Search"); 
     tab3.setTabListener(this); 

     action_bar.addTab(tab1); 
     action_bar.addTab(tab2); 
     action_bar.addTab(tab3); 

     CreditCards cards=new CreditCards(); 

     cards.setArguments(args); 


    } 

    public void switchToFragmentB(){ 
      FragmentTransaction fm=getSupportFragmentManager().beginTransaction();  
      fm.replace(R.id.tabcontent, new Myprofile());   
      fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
      fm.addToBackStack(null); 
      fm.commit(); 


    } 

    @Override 
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 
     viewPager.setCurrentItem(tab.getPosition()); 

    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 


    } 


} 


class MyAdoptor extends FragmentPagerAdapter 
{ 

    public MyAdoptor(FragmentManager fm) { 
     super(fm); 
       // TODO Auto-generated constructor stub 
    } 

    @Override 
    public Fragment getItem(int arg0) { 
     // TODO Auto-generated method stub 

     Fragment fragement=null; 
     if(arg0==0) 
     { 
      fragement=new Loginfrag(); 
     } 
     if(arg0==1) 
     { 
      fragement=new Comparefrag(); 
     } 
     if(arg0==2) 
     { 
      fragement=new Searchfrag(); 
     } 



     System.out.print("<><><><>"); 
     return fragement; 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 3; 
    } 

} 

Comparefrag.java(這是這裏fragement我使用的ImageView調用一個新的fragement,人有我新fragement &它在MainActivity.java已經啓動)

 package com.tabs; 

import com.comparenow.CreditCards; 
import com.example.swipetabs.MainActivity; 
import com.example.swipetabs.R; 
import com.example.swipetabs.R.layout; 

import android.support.v4.app.FragmentTransaction; 
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.ImageView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 



/** 
* A simple {@link android.support.v4.app.Fragment} subclass. 
* 
*/ 
public class Comparefrag extends Fragment implements View.OnClickListener { 

    public Comparefrag() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.comparenow_b, container, false); 
     ImageView btnT = (ImageView)v.findViewById(R.id.imageView2); 
     btnT.setOnClickListener(this); 
     return v; 
    } 

public void onClick(View v) { 

    // FragmentTransaction fm=((MainActivity)getActivity()).getSupportFragmentManager().beginTransaction(); 
    FragmentTransaction fm=getActivity().getSupportFragmentManager().beginTransaction(); 
    CreditCards aFrag = new CreditCards(); 
    fm.replace(android.R.id.tabcontent, aFrag); 
    fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    fm.commit(); 

     System.out.print(">>>>>>>>>>>>"); 

     /*When I click this button in my fragment, I'd like it to go to fragment B for example*/ 
    // ((MainActivity)getActivity()).switchToFragmentB(); 


    } 

} 

CreditCards.java

package com.comparenow; 

import com.example.swipetabs.R; 
import com.example.swipetabs.R.layout; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* A simple {@link android.support.v4.app.Fragment} subclass. 
* 
*/ 
public class CreditCards extends Fragment { 

    public CreditCards() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     return inflater.inflate(R.layout.creditcards_c, container, false); 


    } 

} 

錯誤:登錄貓

02-05 17:10:59.163: E/AndroidRuntime(30400): FATAL EXCEPTION: main 
02-05 17:10:59.163: E/AndroidRuntime(30400): java.lang.IllegalArgumentException: No view found for id 0x1020011 (android:id/tabcontent) for fragment CreditCards{418f6838 #3 id=0x1020011} 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.os.Handler.handleCallback(Handler.java:605) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.os.Looper.loop(Looper.java:137) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at android.app.ActivityThread.main(ActivityThread.java:4511) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at java.lang.reflect.Method.invokeNative(Native Method) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at java.lang.reflect.Method.invoke(Method.java:511) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
02-05 17:10:59.163: E/AndroidRuntime(30400): at dalvik.system.NativeStart.main(Native Method) 
+0

使用本FragmentTransaction FM = getActivity( ).getSupportF 。ragmentManager()的BeginTransaction(); – Palak

+0

@Palak我試過這種方式FragmentTransaction fm =((MainActivity)getActivity())。getSupportFragmentManager()。beginTransaction(); 但即時獲取錯誤爲 ype不匹配:無法從android.support.v4.app.FragmentTransaction轉換爲android.app.FragmentTransaction – VenushkaT

+0

import android.app.FragmentTransaction; n將此替換爲導入android.support.v4.app。 FragmentTransaction; – Palak

回答

2

試一試在點擊事件

FragmentTransaction fm=getSupportFragmentManager().beginTransaction();  
    fm.replace(R.id.tabcontent, new Myprofile());   
    fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    fm.addToBackStack(null); 
    fm.commit(); 
+0

@David當我將此代碼添加到onClick(View v)時,我得到了很多錯誤。 – VenushkaT

+0

你能列出一些錯誤信息嗎?順便說一句:你應該@VenushkaT,我只是格式化的代碼。 – David

+0

@David ype mismatch:無法從android.support.v4.app.FragmentTransaction轉換爲android.app.FragmentTransaction – VenushkaT

1

看看我的演示代碼


package com.tabs; 

import com.comparenow.CreditCards; 
import com.example.swipetabs.MainActivity; 
import com.example.swipetabs.R; 
import com.example.swipetabs.R.layout; 

import android.support.v4.app.FragmentTransaction; 
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.ImageView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 



/** 
* A simple {@link android.support.v4.app.Fragment} subclass. 
* 
*/ 
public class Comparefrag extends Fragment implements View.OnClickListener { 

    public Comparefrag() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.comparenow_b, container, false); 
     ImageView btnT = (ImageView)v.findViewById(R.id.imageView2); 
     btnT.setOnClickListener(this); 
     return v; 
    } 

public void onClick(View v) { 

    // FragmentTransaction fm=((MainActivity)getActivity()).getSupportFragmentManager().beginTransaction(); 
    FragmentTransaction fm=getActivity().getSupportFragmentManager().beginTransaction(); 
    CreditCards aFrag = new CreditCards(); 
    fm.replace(android.R.id.tabcontent, aFrag); 
    fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    fm.commit(); 

     System.out.print(">>>>>>>>>>>>"); 

     /*When I click this button in my fragment, I'd like it to go to fragment B for example*/ 
    // ((MainActivity)getActivity()).switchToFragmentB(); 


    } 

} 
+0

請找到我的新編輯代碼那裏即時獲取新錯誤couldnt intialise getSupportFragmentManager()FragmentTransaction fm = getSupportFragmentManager()。beginTransaction(); (Comparefrag.java) – VenushkaT