当我启动应用程序并输入按钮时,它出现错误“不幸的是,intenttest已经停止。
Fragment_Main.jave代码如下
public class Fragment_Main extends Fragment implements OnClickListener {
EditText studentID;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false);
studentID = (EditText) root.findViewById(R.id.editText1);
Button submit = (Button) root.findViewById(R.id.button1);
submit.setOnClickListener(this);
return root;
}
@Override
public void onClick(View v) {
Fragment frag = new Average();
Intent intent = new Intent(getActivity(), Average.class);
intent.putExtra("ID", studentID.getText().toString());
startActivity(intent);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container,frag).commit();
}}
average.jave代码如下
public class Average extends Fragment {
TextView studentInfo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_average, container, false);
studentInfo = (TextView) root.findViewById(R.id.textView2);
Intent intent = getActivity().getIntent();//取得活動的Intent
studentInfo.setText("學號:" + intent.getStringExtra("ID"));
return root;
}}
答案 0 :(得分:2)
从Fragment您可以使用以下代码
开始活动Intent intent = new Intent(getActivity(),yourclassname.class);
startActivity(intent);