我有问题。我试图在用户点击它时更改片段,但它不起作用。我没有看到错误,但它适用于另一个片段。这是我的代码:
**我尝试切换的片段:**
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_profil, container, false);
TextView textViewNom = view.findViewById(R.id.labelNom);
textViewNom.setText(((Main)getActivity()).getNomComplet());
linearScore = (LinearLayout)view.findViewById(R.id.linearScore);
linearScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment_score frag_score= new fragment_score();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, frag_score,"scorefrag");
transaction.addToBackStack("scorefrag");
transaction.commit();
}
});
linearReglage = (LinearLayout)view.findViewById(R.id.linearReglage);
linearReglage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment_reglage frag_reglage= new fragment_reglage();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, frag_reglage,"reglagefrag");
transaction.addToBackStack("reglagefrag");
transaction.commit();
}
});
要解释一下,当我在视图中点击linearScore时我没有问题,但是当我尝试点击linearReglage时我的应用程序重新启动。
linearReglage(XML):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="layout.fragment_reglage">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="oui"
android:textSize="30dp"/>
</ScrollView>
fragment_reglage(java):
public class fragment_reglage extends Fragment {
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_reglage, container, false);
return view;
}
public void onResume(){
super.onResume();
}
}
我没有看到错误我只有一个失败的异常(因为非0退出状态)。但应用程序重新启动就好像它是基本的。 我知道这是什么错误,如果是链接
答案 0 :(得分:1)
在评论中@Eselfar建议您查看此answer
使用dex解决方案并在真实设备上尝试此缓存错误可能早已消失!但对我来说,除了您尝试使用backStack沿着添加选项
的缓存机制外,代码中的一切似乎都很好请参阅 this 以了解后台管理
我认为这段代码可以解决你的问题
只需将fragmentTransaction.add更改为fragmentTransaction.replace,因为从您的代码中尝试在两个片段之间导航替换将保持缓存
这样做并在真实设备上进行测试..
这里代码在更改后的样子如何
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_profil, container, false);
TextView textViewNom = view.findViewById(R.id.labelNom);
textViewNom.setText(((Main)getActivity()).getNomComplet());
linearScore = (LinearLayout)view.findViewById(R.id.linearScore);
linearScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment_score frag_score= new fragment_score();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, frag_score,"scorefrag");
transaction.addToBackStack("scorefrag");
transaction.commit();
}
});
linearReglage = (LinearLayout)view.findViewById(R.id.linearReglage);
linearReglage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment_reglage frag_reglage= new fragment_reglage();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, frag_reglage,"reglagefrag");
transaction.addToBackStack("reglagefrag");
transaction.commit();
}
});