将null赋给类静态变量导致NullPointerException

时间:2015-07-21 02:24:12

标签: android nullpointerexception static-members

public class ClassHoler {
    public static List<Car> carsHolder;
}

在我使用过的活动中。

@Override
public void onClick(View v) {
    ClassHoler.carsHolder = null;
}

在我的应用中,我在java.lang.NullPointerException上获得了ClassHoler.carsHolder = null; 怎么会这样?

logcat的:

java.lang.NullPointerException
com.souche.fengche.ui.activity.workbench.customer.AddCustomerActivity.void onClick(android.view.View)(SourceFile:282)
android.view.View.performClick(View.java:4472)
android.view.View$PerformClick.run(View.java:18795)
android.os.Handler.handleCallback(Handler.java:808)
android.os.Handler.dispatchMessage(Handler.java:103)
android.os.Looper.loop(Looper.java:193)
android.app.ActivityThread.main(ActivityThread.java:5332)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
dalvik.system.NativeStart.main(Native Method)

源文件:

  @Override
    public void onClick(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(name.getWindowToken(), 0);

        switch (v.getId()) {
            case R.id.gentle_man:
                check(gentleManRadio);
                unCheck(ladyRadio);

                break;
            case R.id.lady:
                check(ladyRadio);
                unCheck(gentleManRadio);

                break;
            case R.id.source:
                getSources();
                mPicker.showAtLocation(parent, Gravity.CENTER, 0, 0);
                break;
            case R.id.customer_requirement_remark:
                Intent intent = new Intent(this, AddRequireRemarkActivity.class);
                intent.putExtra(Constant.USER_ID, userID);
                startActivityForResult(intent, ADD_REMARK);

                break;
            case R.id.yes:
                check(yesRadio);
                unCheck(noRadio);
                watchedCars.setVisibility(View.VISIBLE);
                line.setVisibility(View.VISIBLE);
                break;
            case R.id.no:
                check(noRadio);
                unCheck(yesRadio);
                watchedCars.setVisibility(View.GONE);
                line.setVisibility(View.GONE);
                break;
            case R.id.watched_cars:
                intent = new Intent(this, AddWatchedCarActivity.class);
                intent.putExtra(Constant.CAR_ID, mWatchedCarsString);
                startActivity(intent);
                break;
            case R.id.level:
                getLevels();
                break;
            case R.id.next_visit_time:
                mSelectDate.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.save_my_cusotmer:
                if (name.getText().toString().trim().length() > 5) {
                    name.setError("名字不能超过五个字");
                    return;
                }

                //clear class holder
282   ------>   ClassHoler.carsHolder = null;

                mLoadingDialog.show();
                saveMyCustomer();
                break;
        }

    }

只在这里使用:

 private void initView() {
        mLoadingDialog = new LoadingDialog(this);
        List<Car> cars;

        if (ClassHoler.carsHolder == null) {
            cars = new ArrayList<>();
        } else {
            cars = ClassHoler.carsHolder;
            ClassHoler.carsHolder = null;
        }
    }

我的代码是否有一些极端条件得到NullPointerException?

0 个答案:

没有答案