Android未决意图
我的活动中有一个viewpager。在viewpager中,有一个片段,该片段包含recyclerview。收到通知后,我想在我的recyclerview中添加新项目,并想突出显示它 活动类别:Android未决意图,android,android-fragments,Android,Android Fragments,我的活动中有一个viewpager。在viewpager中,有一个片段,该片段包含recyclerview。收到通知后,我想在我的recyclerview中添加新项目,并想突出显示它 活动类别: protected void onNewIntent(Intent intent) { super.onNewIntent(intent); boolean not_id = intent.getBooleanExtra(AppConstants.NEW_NOTIFICATION, fa
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
boolean not_id = intent.getBooleanExtra(AppConstants.NEW_NOTIFICATION, false);
String bid_id = intent.getStringExtra(AppConstants.BID_NOTIFICATION_ID);
if(not_id){
ActiveBidFragment frag = ActiveBidFragment.newInstance(bid_id, "");
frag.setBid_id(bid_id);
frag.newBidReceived();
frag.setBid_id("0");
Log.i(TAG,"~~~~done refreshing");
}
}
片段:
public void newBidReceived(){
fetchActiveBids();
}
private void fetchActiveBids(){
if(mActiveBidPresenter == null)
mActiveBidPresenter = new ActiveBidPresenterImpl(this);
mActiveBidPresenter.getActiveBids();
}
@Override
public void onFetchedActiceBidsSuccess() {
if(bids == null)
bids = new ArrayList<ActiveBid>();
bids.clear();
bids.addAll(AppContext.getInstance().getActiveBids());
adapter.notifyDataSetChanged();
}
在对话框中,我将错误显示为nullpointerexception,它将getActivity显示为null。
是否有其他方法可以实现我试图实现的目标,可能类似于重新创建活动的整个实例(如果活动已经存在)
更新:
将片段创建更改为:
ActiveBidFragment frag = (ActiveBidFragment) adapter.getItem(0);
frag.setBid_id(bid_id);
frag.newBidReceived();
frag.setBid_id("0");
//adapter is the viewpagerpadapter to which fragment is attached
您的片段将获得
getActivity()
始终为空,除非您的片段附加了活动
就你而言:
ActiveBidFragment frag = ActiveBidFragment.newInstance(bid_id, "");
frag.setBid_id(bid_id);
frag.newBidReceived();
frag没有附加任何活动。我在fragmentpageradapter内的hashmap中维护fragment实例,然后在我的onNewIntent中引用它。这是我能得到的唯一解决办法。它就像一个单体模式,直到活动活跃。如果有人发布了更好的解决方案,我很想知道它在你的onNewIntent函数中,你正在创建的新片段是在本地声明的,一旦你离开那些花括号,即在日志行之后,它就会被销毁,由于片段的作用域仅限于这些大括号内,因此如何在父活动内获取当前片段实例。注意:我的布局文件不包含任何片段标记。或者我怎样才能达到我想要达到的目标。顺便说一句,只有在调用showProgressBar的时间和位置,showProgressBar才会在该循环中被调用?添加了整个代码导致该问题的确切错误消息和代码行是什么?是的,我知道,片段的新实例导致了该问题,我已经在注释中提到过。我该如何解决这个问题,我应该将片段设置为singleton吗?@mayank我认为这不是一个好的做法,我建议您可以等待在片段的onCreate()函数实现中调用getActivity()/newidReceived()stuff。修改了片段实例化的代码,仍然得到相同的错误。你能看一下吗
@Override
public void fetchActiveBids(String vendor_id, OnActiveBidFinished mListener) {
String url = AppConstants.ACTIVE_BID_URL;
Map<String,String> params = new HashMap<String, String>();
params.put(AppConstants.VENDOR_ID, vendor_id);
JsonObjectRequest loginRequest = new JsonObjectRequest(Request.Method.POST,
url,
new JSONObject(params),
createMyReqSuccessListener(mListener),
createMyReqErrorListener(mListener));
VolleySingleton.getRequestQueue().add(loginRequest);
}
public static ProgressDialog generateWaitingDialog(Context context, String message){
ProgressDialog progressDialog = new ProgressDialog(context, AlertDialog.THEME_HOLO_LIGHT);
progressDialog.setCancelable(false);
progressDialog.setMessage(message);
return progressDialog;
}
ActiveBidFragment frag = (ActiveBidFragment) adapter.getItem(0);
frag.setBid_id(bid_id);
frag.newBidReceived();
frag.setBid_id("0");
//adapter is the viewpagerpadapter to which fragment is attached
ActiveBidFragment frag = ActiveBidFragment.newInstance(bid_id, "");
frag.setBid_id(bid_id);
frag.newBidReceived();