我在我的應用程序中有兩個活動。在MainActivity
中我加載了2個片段Fragment1
和Fragment2
。從Fragment1
我正在跳躍Fragment2
和Fragment2
我跳到Activity2
包含Fragment2_1
從Fragment 2_1
我想跳回到Fragment2
在MainActivity
但是當我嘗試這樣做,我得到一個錯誤。來自不同活動的兩個片段之間的通信?
我想知道是否有可能從1個活動片段跳到另一個活動片段?
我的代碼是:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
Fragment fragment1=new Fragment1();
fragmentTransaction.replace(R.id.mainContainer,fragment1,"MainActivity");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Fragment1.java:
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_fragment1, container, false);
Button buttonfrag1=(Button)view.findViewById(R.id.buttonfrag1);
buttonfrag1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.mainContainer,fragment2,"Fragment1");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
Fragment2.java
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_fragment2, container, false);
Button buttonfrag2=(Button)view.findViewById(R.id.buttonfrag2);
buttonfrag2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(getActivity(),Activity2.class);
startActivity(intent);
}
});
return view;
}
}
Activity2.java
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
Fragment2_1 fragment2_1=new Fragment2_1();
fragmentTransaction.replace(R.id.frag2_1,fragment2_1,"Activity2");
fragmentTransaction.commit();
}
}
Fragment2_1.java
public class Fragment2_1 extends Fragment {
public Fragment2_1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_fragment2_1, container, false);
Button buttonfrag2_1=(Button)view.findViewById(R.id.buttonfrag2_1);
buttonfrag2_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
Fragment2 fragment2=new Fragment2();
fragmentTransaction.replace(R.id.mainContainer,fragment2,"frag2_1");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
所有的代碼工作正常,但是當我想從Fragment2_1移動到片段1我的應用程序崩潰與一個例外:
java.lang.IllegalArgumentException: No view found for id 0x7f0b0059 (saleskit.orbitsys.com.fragmentdemo:id/mainContainer) for fragment Fragment2{29eb9c90 #1 id=0x7f0b0059 frag2_1}
你有答案嗎?在下面檢查我的答案。 @champadorid –