空指针调试

时间:2019-05-30 09:00:34

标签: android

我要做什么的简短摘要。

使用共享的首选项,以便当用户观看视频时,视频旁边会出现一个绿色的勾号,将其标记为已完成。当前收到空指针错误。

基本上有两个页面-主页和视频页面。当用户登陆到主页上时,if语句应检查共享首选项,以查看他们是否已经访问了视频页面。如果有,则应显示completedTick imageView。

请参见下面的代码和上下文。

主页面代码片段:

.pyc

视频页代码片段:

 /**
 * Shared preferences variable which is used to store the userId which is captured at the login screen.
 */
private SharedPreferences loginPref, activityPref;   
private ImageView completedTick;
private Boolean activityCompleted;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
    activityPref = getContext().getSharedPreferences("activityPref", Context.MODE_PRIVATE);
    activityCompleted = activityPref.getBoolean("activityCompleted", true);

    View view = inflater.inflate(R.layout.fragment_activities, container, false);

    initialiseRecyclerView(view);
    retrieveActivities();

    if(activityCompleted == false || activityCompleted == null ) {
        completedTick.setVisibility(view.INVISIBLE);
    }else{
        completedTick.setVisibility(View.VISIBLE);
    }

    return view;

}

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

/**
 * Method which is used to initialise the recycler view
 *
 * @param view
 */
private void initialiseRecyclerView(View view) {
    mRecyclerView = view.findViewById(R.id.recyclerViewActivities);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    EmptyAdapter emptyAdapter = new EmptyAdapter();
    mRecyclerView.setAdapter(emptyAdapter);
    completedTick = view.findViewById(R.id.completedTick);
}

错误是:

private SharedPreferences activityPref;
private Boolean activityCompleted;


@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.youtube_layout);

    Log.d(TAG, "onCreateView: Starting");
    activityPref = getSharedPreferences("activityPref", MODE_PRIVATE);
    activityCompleted = activityPref.getBoolean("activityCompleted", true);

        getExtrasMethod();
        initialiseViews();
        setTextInViews();
        checkCompleteD();


 private void checkCompleteD() {

    if (activityCompleted) {
        SharedPreferences.Editor editor = activityPref.edit();
        activityCompleted = false;
        editor.putBoolean("activityCompleted", activityCompleted);
        editor.apply();

    }
}

更新:

进行建议的更改

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
        at com.example.fit.ActivitiesFragment.onCreateView(ActivitiesFragment.java:78)

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setVisibility(int)'         在com.example.feelingfit.ActivitiesFragment.onViewCreated(ActivitiesFragment.java:85)

这行是哪一行:completedTick.setVisibility(View.INVISIBLE);

2 个答案:

答案 0 :(得分:5)

首先,您的completedTick为空。尝试将completedTick = view.findViewById(R.id.completedTick);放在您的OnViewCreated代码中上方

if(activityCompleted == false || activityCompleted == null ) {
        completedTick.setVisibility(View.INVISIBLE);
    }else{
        completedTick.setVisibility(View.VISIBLE);
    }

此外,将view.INVISIBLE替换为View.INVISIBLE

编辑+附加信息:

如果再次出现NullPointerException,请参阅this link

修改2: 像这样在onViewCreated方法内重写代码:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_activities, container, false);

}

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
 loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
        activityPref = getContext().getSharedPreferences("activityPref", Context.MODE_PRIVATE);
        activityCompleted = activityPref.getBoolean("activityCompleted", true);

        initialiseRecyclerView(view);
        retrieveActivities();

        if(activityCompleted == false || activityCompleted == null ) {
            completedTick.setVisibility(View.INVISIBLE);
        }else{
            completedTick.setVisibility(View.VISIBLE);
        }
}

编辑编号3:由于在同一用例上它再次崩溃,因此我假设您的R.id.completedTickImage在您的fragment_activities不是

答案 1 :(得分:0)

CompleteTick.setVisibility方法中执行onActivityCreated()的操作,并将view.INVISIBLE替换为View.INVISIBLE