错误:尝试在空对象引用上调用虚拟方法'android.content.Context.getResources()'

时间:2018-06-19 15:16:11

标签: java android nullpointerexception

在阅读本文之前,大家好。我有一个问题,当我通常看到logcat时,logcat会在类活动/片段/或我创建的类中给出信息错误行。但是我在logcat中感到奇怪,它没有显示行错误代码。

我的日志猫

06-19 22:50:31.441 3980-3980/com.tenagakerja.tenagakerja E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tenagakerja.tenagakerja, PID: 3980
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tenagakerja.tenagakerja/com.tenagakerja.tenagakerja.ui.login.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
    at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
    at android.view.Window.getDefaultFeatures(Window.java:1306)
    at android.view.Window.<init>(Window.java:453)
    at com.android.internal.policy.impl.PhoneWindow.<init>(PhoneWindow.java:290)
    at com.android.internal.policy.impl.Policy.makeNewWindow(Policy.java:60)
    at com.android.internal.policy.PolicyManager.makeNewWindow(PolicyManager.java:57)
    at android.app.Activity.attach(Activity.java:5932)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是我的活动代码:

public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> implements LoginNavigator {
    @Inject
    public LoginViewModel loginViewModel;

    private ActivityLoginBinding activityLoginBinding;

    @Override
    public int getBindingVariable() {
        return BR.loginViewModel;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginViewModel getViewModel() {
        return loginViewModel;
    }

    @Override
    public void handleError(Throwable throwable) {
        Timber.d(throwable);
    }

    @Override
    public void login() {
        String email = activityLoginBinding.loginEditEmail.getText().toString();
        String password = activityLoginBinding.loginEditPassword.getText().toString();

        loginViewModel.login(email, password);
    }

    @Override
    public void openMainActivity() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLoginBinding = getViewDataBinding();
        loginViewModel.setNavigator(this);

//        Toolbar toolbar = findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
    }

}

基本活动

public abstract class BaseActivity<T extends ViewDataBinding, V extends BaseViewModel> extends AppCompatActivity implements BaseFragment.Callback{

    private ProgressDialog progressDialog;
    private T viewDataBinding;
    private V viewModel;

    public abstract int getBindingVariable();

    public abstract @LayoutRes int getLayoutId();

    public abstract V getViewModel();

    @Override
    public void onFragmentAttached() {

    }

    @Override
    public void onFragmentDetached(String tag) {

    }

    @Override
    protected void attachBaseContext(Context newBase) {

    }

    @Override
    protected void onCreate(@Nullable Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        performDependencyInjection();
        performDataBinding();

    }

    public T getViewDataBinding() {
        return viewDataBinding;
    }

    @TargetApi(Build.VERSION_CODES.M)
    public boolean hasPermission(String permission) {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||checkSelfPermission(permission)
                == PackageManager.PERMISSION_GRANTED;
    }

    public void hideKeyboard() {
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
            }
        }
    }

    public void hideLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
        }
    }

    public boolean isNetworkConnected() {
        return NetworkUtils.isNetworkConnected(getApplicationContext());
    }

    public void performDependencyInjection() {
        AndroidInjection.inject(this);
    }

    @TargetApi(Build.VERSION_CODES.M)
    public void requestPermissionSafely(String[] permission, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permission, requestCode);
        }
    }

    public void showLoading() {
        hideLoading();
        progressDialog = CommonUtils.showLoadingDialog(getApplicationContext());
    }

    private void performDataBinding() {
        viewDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
        this.viewModel = viewModel == null ? getViewModel() : viewModel;
        viewDataBinding.setVariable(getBindingVariable(), viewModel);
        viewDataBinding.executePendingBindings();
    }
}

1 个答案:

答案 0 :(得分:0)

已解决

我取消注释attachBaseContext,是的,该错误消失了:D