我想使用" main"从一个片段跳到另一个片段。作为我的容器,但我一直得到"不幸的是,应用程序停止了#34;错误信息。这是代码:
package com.wiss.clic.clickit;
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 Fragment} subclass.
*/
public class MainFragment extends Fragment {
public MainFragment() {
// 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.fragment_main, container, false);
}
public void Click(View v){
getFragmentManager().beginTransaction().replace(R.id.main, new GameFragment()).commit();
}
}
答案 0 :(得分:0)
使用getSupportFragmentManager()代替getFragmentManager()因为你的扩展 [import android.support.v4.app.Fragment] support-v4 not [import android.app.Fragment;]
getSupportFragmentManager()。beginTransaction()。replace(R.id.main,new GameFragment())。commit();
其他从导入android.support.v4.app.Fragment替换包名称以导入android.app.Fragment;