我已经在我的Android应用程序中实现了一个ListView。我使用ArrayAdapter类的自定义子类绑定到这个ListView。在重写的ArrayAdapter.getView(…)方法中,我分配了一个OnClickListener。在OnClickListener的onClick方法中,我想启动一个新活动。我得到了一个异常:

Calling startActivity() from outside of an Activity  context requires the  
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

我怎样才能得到ListView(当前活动)正在工作的上下文?


当前回答

另外:如果你在listview中以片段的形式显示链接,不要这样创建它

adapter = new ListAdapter(getActivity().getApplicationContext(),mStrings);

而不是打电话

adapter = new ListAdapter(getActivity(),mStrings);

适配器在这两种情况下都工作得很好,但链接只在最后一种情况下工作。

其他回答

另外:如果你在listview中以片段的形式显示链接,不要这样创建它

adapter = new ListAdapter(getActivity().getApplicationContext(),mStrings);

而不是打电话

adapter = new ListAdapter(getActivity(),mStrings);

适配器在这两种情况下都工作得很好,但链接只在最后一种情况下工作。

来自Xamarin的人。form或Xamarin。安卓,在你的Xamarin里。Android项目,使用:

Xamarin.Essentials.Platform.CurrentActivity.StartActivity(intent);

注意,这可能需要Xamarin。Essentials v1.5或以上版本

正如@Alex Volovoy所提到的,应该避免设置标志,因为它会干扰事件和历史堆栈的正常流程。

在我看来,最好在Activity.class的代码中使用startActivity()方法。如果您在适配器或其他类中使用它,则会导致该结果。

CustomAdapter mAdapter = new CustomAdapter( getApplicationContext(), yourlist);

or

Context mContext = getAppliactionContext();
CustomAdapter mAdapter = new CustomAdapter( mContext, yourlist);

更改如下

CustomAdapter mAdapter = new CustomAdapter( this, yourlist);
Intent i= new Intent(context, NextActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);