android中fragment跳转到指定activity的问题
源码如下:publicclassFirstFragmentextendsFragment{publicViewonCreateView(LayoutInflaterinf...
源码如下:
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
TextView bt_first = (TextView) view.findViewById(R.id.tv_first);
//bt_first.setText(MainActivity.temp);
bt_first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(getActivity(),NumPickerActivity.class);
startActivity(intent);
}
});
return view;
}
}
不知道为什么跳转跳不过去。log显示错误在于startActivity(intent);
而当我换一个指定类,却可以
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
TextView bt_first = (TextView) view.findViewById(R.id.tv_first);
//bt_first.setText(MainActivity.temp);
bt_first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(getActivity(),Activity_Second.class);
startActivity(intent);
}
});
return view;
}
}
但是Activity_Second.class和NumPickerActivity.class内容是一样的。求大神解答QAQ 展开
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
TextView bt_first = (TextView) view.findViewById(R.id.tv_first);
//bt_first.setText(MainActivity.temp);
bt_first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(getActivity(),NumPickerActivity.class);
startActivity(intent);
}
});
return view;
}
}
不知道为什么跳转跳不过去。log显示错误在于startActivity(intent);
而当我换一个指定类,却可以
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
TextView bt_first = (TextView) view.findViewById(R.id.tv_first);
//bt_first.setText(MainActivity.temp);
bt_first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(getActivity(),Activity_Second.class);
startActivity(intent);
}
});
return view;
}
}
但是Activity_Second.class和NumPickerActivity.class内容是一样的。求大神解答QAQ 展开
1个回答
2015-09-09 · 知道合伙人软件行家
关注
展开全部
fragment是运行在Activity中的,转跳到其它Activity需要用到Context的API。
具体实现方式:
获取装载fragment的Activity做为Context
getActivity() 得到Context
创建Intent 意图,并指定转跳的Activity
Context context = getActivity();
Intent intent = new Intent(context ,目标Activity.class);
调用context.startActivity(intent);即可转跳到指定的Activity
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询