我在主要活动中有一个片段来处理我活动的所有操作。
当我使用片段属性时,很少发生NullPointerException:
片段代码:
public class ProjectInfoFragment extends Fragment {
private static final String ARG_PID = "pid";
private int projectId;
View view;
public ProjectInfoFragment() {
// Required empty public constructor
}
public static ProjectInfoFragment newInstance(int pid) {
ProjectInfoFragment fragment = new ProjectInfoFragment();
Bundle args = new Bundle();
args.putInt(ARG_PID, pid);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
projectId = getArguments().getInt(ARG_PID);
}
}
@Override
public void onResume() {
super.onResume();
mainAction();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_project_info, container, false);
return view;
}
private void mainAction() {
ProjectModel projectModel = new ProjectModel(getContext());
if (!projectModel.findByKey(projectId + "")) { //// line 121
//!!!!!!! here NullPointerException occurs !!!!!!
return;
}
}
}
重要提示:我在mainAction()中做了很多事情,必须在onResume()中完成,我不能在createView或其他函数中执行此操作。
日志:
java.lang.NullPointerException: Attempt to invoke virtual method boolean ir.rabint.shahta.model.ProjectModel.findByKey(java.lang.String) on a null object referencen
at ir.rabint.shahta.activity.fragment.ProjectInfoFragment.mainAction(ProjectInfoFragment.java:121)
at ir.rabint.shahta.activity.fragment.ProjectInfoFragment.onResume(ProjectInfoFragment.java:92)
at android.support.v4.app.Fragment.performResume(Fragment.java:2238)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1346)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.uteOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.uteOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.uteOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAnduteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.PendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
答案 0 :(得分:0)
!
运算符仅适用于Boolean
数据类型/方法。在这里,您将比较堆栈跟踪中所述的String。
以下是
if(projectModel != null){
if(!projectModel.findByKey(projectId + "").equals("")){
return;
}
}else{
//do whatever you want
}