我有一个有自己布局的片段。在布局中,有一个listview,我附加了onitemclick监听器,它将在单击列表行时启动/打开意图。不幸的是,我总是收到这个错误:
从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志
但我不喜欢设置标志活动。我无法打开SamplePage.class
public class FrontPageFragment extends Fragment
{
private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView=inflater.inflate(R.layout.frontpage, container, false);
m_orders = new ArrayList<Order>();
this.m_adapter = new OrderAdapter(this.getActivity().getApplicationContext(), R.layout.name, m_orders);
View eView=(View)fragmentView.findViewById(R.id.approvedOrders);
ListView listView=(ListView)eView.findViewById(R.id.listview);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(view.getContext(), SamplePage.class);
intent.putExtra("id", "1");
view.getContext().startActivity(intent);
}
});
//bind data
listView.setAdapter(this.m_adapter);
return fragmentView;
}
答案 0 :(得分:12)
当我复制你的代码时,我得到了同样的错误。所以我稍微改了一下,它起作用了。检查一下:
Intent intent = new Intent(getActivity().getBaseContext(), Sampleclass.class);
intent.putExtra("id", 1);
startActivity(intent);
答案 1 :(得分:1)
试试这个:
FrontPageFragment.this.startActivity(intent);