问题是我的应用在尝试添加和删除活动片段时在某些情况下崩溃了。添加和删除片段代码如下:
private void popFragment() {
//Load animation
Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
flBranch.startAnimation(slide_up);
flBranch.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS|WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Log.d("Tag", "Fragment Transaction Commit");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_branch, new BranchFragment());
ft.commit();
}
private void removeFragmentBroadcast() {
IntentFilter intentFilter = new IntentFilter("REMOVE_FRAGMENT");
broadcastReceiverTab = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Branch detail
BranchModel bm = new Gson().fromJson(intent.getStringExtra("branch_address"), BranchModel.class);
//Change text
if (bm != null) {
txtBranchAddress.setText(bm.getLocation());
txtBranchName.setText(bm.getName());
}
//Set new values to offer model
offerModel.setPhone(bm.getPhone());
offerModel.setLatitude(bm.getLatitude());
offerModel.setLongitude(bm.getLongitude());
//Remove fragment with animation
Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_down);
flBranch.startAnimation(slide_down);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
getSupportFragmentManager().beginTransaction().
remove(getSupportFragmentManager().findFragmentById(R.id.fl_branch)).commit();
flBranch.setVisibility(View.GONE);
}
};
context.registerReceiver(broadcastReceiverTab, intentFilter);
}
我还取消了在onDestroy上广播的注册
@Override
protected void onDestroy() {
super.onDestroy();
context.unregisterReceiver(broadcastReceiverTab);
}
这就是我如何将广播发送到有关单击/关闭项目的活动
imgCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("REMOVE_FRAGMENT");
intent.putExtra("branch_address", new Gson().toJson(mList.get(pos)));
context.sendBroadcast(intent);
}
});
以上代码在我的项目中提供了详细信息活动。我直接打开 提供房屋报价清单中的详细信息,并尝试打开和关闭分支机构 片段工作正常。但是如果我从供应商列表转到供应商 详细信息,其中还包含要约列表,然后我要提供详细信息 并尝试打开关闭分支片段,我的应用程序崩溃了。 进行调试,因为在onReceive中控件将移动到的结束代码 onReceive。崩溃如下:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextAnim(int)' on a null object reference
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:768)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6238)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)