空返回错误(空指针异常)

时间:2017-12-18 13:42:10

标签: android

我是Android新手。我一直在尝试编写一些视图,事件处理程序以及共享首选项类。在下面的代码中,我收到以下错误:

" java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android.widget.EditText.getText()'在null对象引用"

我已经多次检查过我的代码,但是我无法找到我调用空对象的代码。请帮忙。

主要课程:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.button);
        EditText username = (EditText) findViewById(R.id.editText);
        EditText password = (EditText) findViewById(R.id.editText2);
        b.setOnClickListener(this);
        TextView displayText = findViewById(R.id.textView);
        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();

        if(storedUsername == null){
            storedUsername = pref.getString("username", "empty");

        }
        if (storedPassword == null){
            storedPassword = pref.getString("password", "empty");
        }

        displayText.setText(storedUsername + " " + storedPassword);

    }



    @Override
    public void onClick(View view) {


        editor.putString("username", username.getText().toString());
        editor.putString("password", password.getText().toString());
        editor.commit();




    }
}

XML布局:

<?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.czolb.persistence.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="148dp"
        android:layout_marginStart="148dp"
        android:layout_marginTop="38dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="84dp"
        android:layout_marginStart="85dp"
        android:layout_marginTop="73dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="84dp"
        android:layout_marginStart="85dp"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="163dp"
        android:layout_marginStart="163dp"
        android:layout_marginTop="60dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

您的onClick(View view)方法无法访问您声明的onCreate()方法变量:

EditText username = (EditText) findViewById(R.id.editText);
EditText password = (EditText) findViewById(R.id.editText2);

将他们从方法中取出并在你的课堂内宣布。

您的代码应该如下所示:

--YourClassName-- {

    EditText username;
    EditText password;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.button);
        username = (EditText) findViewById(R.id.editText);
        password = (EditText) findViewById(R.id.editText2);
        b.setOnClickListener(this);
        TextView displayText = findViewById(R.id.textView);
        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();

        if(storedUsername == null){
            storedUsername = pref.getString("username", "empty");
        }

        if (storedPassword == null){
            storedPassword = pref.getString("password", "empty");
        }

        displayText.setText(storedUsername + " " + storedPassword);
    }

    @Override
    public void onClick(View view) {

        editor.putString("username", username.getText().toString());
        editor.putString("password", password.getText().toString());
        editor.commit();

    }
}