我想在我的Android項目中使用。不工作Butterknife空指針異常錯誤
我有一個Activity
,實現Interface
。我正在使用MVP模式。 我正在嘗試從EditText
中獲取文本,但它會拋出一個
空指針異常。
這是我的簡單代碼。
搖籃依賴性
compile 'com.jakewharton:butterknife:8.8.1'
佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rao.myapplication.LoginActivity">
<EditText
android:id="@+id/etUseName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/etUseName"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="loginClick"
app:layout_constraintTop_toBottomOf="@+id/etPassword"/>
</android.support.constraint.ConstraintLayout>
活動
package com.example.rao.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.rao.myapplication.Presenter.LoginActivityPresenter;
import com.example.rao.myapplication.View.LoginActivityView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class LoginActivity extends AppCompatActivity implements LoginActivityView {
@BindView(R.id.etUseName)
EditText userNameET;
@BindView(R.id.etPassword)
EditText passwordET;
@BindView(R.id.btnLogin)
Button loginBtn;
private LoginActivityPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
passwordET.setText("abcd"); // Fails here as well
presenter = new LoginActivityPresenter(this);
}
@Override
public void navigateToListActivity() {
}
@Override
public void loginFailed() {
}
public void loginClick(View view) {
Toast.makeText(this, userNameET.getText().toString(), Toast.LENGTH_SHORT).show(); // Fails here
Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
}
}
以下是錯誤日誌
致命異常:主 工藝:com.example.rao.myapplication,PID:24847 了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.example.rao .myapplication/com.example.rao.myapplication.LoginActivity}: java.lang.NullPointerException:嘗試調用虛方法'void android.widget.EditText.setText(java.lang.CharSequence)'null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java。 lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :1410) 引起的:java.lang.NullPointerException:嘗試在null對象引用 上調用虛擬方法'void android.widget.EditText.setText(java.lang.CharSequence)'com.example.rao.myapplication.LoginActivity.onCreate( (Activity.read.java:34) at android.app.Activity.performCreate(Activity.java:6942) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:2880) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.Activity螺紋$ H。在android.app.ActivityThread.main處的android.os.Looper.loop(Looper.java:154) 處使用handleMessage(ActivityThread.java:1631) (android.os.Handler.dispatchMessage(Handler.java:102) (本地方法) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal .os.ZygoteInit.main(ZygoteInit.java:1410)
請您請指導我在這個正確的方向,請。
安置自己的LoginActivityView類也。 – Ankita