我已经在个人Android应用程序中实现了ListView
。 我使用ArrayAdapter
类的自定义子类绑定到此ListView
。 在重写的ArrayAdapter.getView(...)
方法内部,我分配了一个OnClickListener
。 我想在OnClickListener
的onClick
方法中启动一个新活动。 我获得例外: ide
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
如何获取ListView
(当前Activity
)在其下工做的Context
? ui
您能够使用addFlags代替setFlags
来实现 this
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
根据文档,它能够: url
向意图添加其余标志(或使用现有标志值)。 spa
编辑 code
请注意,若是您使用的标志会更改历史记录堆栈,如Alex Volovoy的回答所述 : 事件
...避免设置标志,由于它会干扰事件和历史记录堆栈的正常流程。 文档
对于即便在活动中调用StartActivity的状况下,也能够在Xamarin.Android(MonoDroid)上得到此权限的人-这其实是具备新ART运行时的Xamarin错误,请参阅https://bugzilla.xamarin.com/show_bug.cgi?id=17630 get
CustomAdapter mAdapter = new CustomAdapter( getApplicationContext(), yourlist);
要么 it
Context mContext = getAppliactionContext(); CustomAdapter mAdapter = new CustomAdapter( mContext, yourlist);
转到下面
CustomAdapter mAdapter = new CustomAdapter( this, yourlist);
我认为,最好在Activity.class
的代码中使用startActivity()
方法。 若是在Adapter
或其余类中使用它,则会致使这种状况。
另外:若是您在片断视图的列表视图中显示连接,请不要这样建立连接
adapter = new ListAdapter(getActivity().getApplicationContext(),mStrings);
代替打电话
adapter = new ListAdapter(getActivity(),mStrings);
适配器在两种状况下均能正常工做,但连接仅在最后一种状况下有效。