我有一个四个片段(在两个包中)和相应的四个xml文件。其中三个我根据tabselect 设置了 > .fragement(ComapreFrag.java)有ImagineView button
。当我按下它时,我想启动新的碎片(* CreditCards.java *)。怎么做呢。请帮助我。请在下面找到我使用的代码。
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 (这是我在这里使用imageView来调用一个新的例子 - 我有新的fragement&amp;它已经在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);
}
}
错误:log cat
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 :(得分:2)
在点击事件中尝试此操作
FragmentTransaction fm=getSupportFragmentManager().beginTransaction();
fm.replace(R.id.tabcontent, new Myprofile());
fm.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fm.addToBackStack(null);
fm.commit();
答案 1 :(得分: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();
}
}