从Android SQLite DB打印时为空引用

时间:2018-05-20 21:51:26

标签: android database sqlite textview

我试图在按下按钮后将数据库中的条目打印到活动的TextView。这些条目来自用户填写的几个EditText框,并在发送到数据库之前进行了验证。 我不确定为什么,但按下按钮时应用程序崩溃并提供此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"

由于验证我知道它不应该为空,因为它在允许用户前进之前检查空白条目。

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

    portalSqliteHelper = new PortalSqliteHelper(this);

    initViews();

    buttonPortal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            if (validate()) {
                String Name = editTextName.getText().toString();
                String Desc = editTextDesc.getText().toString();

                if (!portalSqliteHelper.isNameExists(Name)) {
                    portalSqliteHelper.addPortal(new Portal(Name, Desc));
                    Snackbar.make(buttonPortal, "Added successfully! ", Snackbar.LENGTH_LONG).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            onUserPortalActivity(view);
                        }
                    }, Snackbar.LENGTH_LONG);

                    textViewPortal.setText(Name); // This is the line it seems to be upset about
                }else {
                    Snackbar.make(buttonPortal, "That name already exists! ", Snackbar.LENGTH_LONG).show();
                }
            }
        }
    });
}

public void onUserPortalActivity(View view) {
    Intent intent = new Intent(this, UserPortalActivity.class);
    startActivity(intent);
}

private void initViews() {
    editTextName = findViewById(R.id.editTextName);
    editTextDesc = findViewById(R.id.editTextDesc);
    textInputLayoutName = findViewById(R.id.textInputLayoutName);
    textInputLayoutDesc = findViewById(R.id.textInputLayoutDesc);

    buttonPortal = findViewById(R.id.buttonPortal);
}

public boolean validate() {
    boolean valid = false;
    String Name = editTextName.getText().toString();
    String Desc = editTextDesc.getText().toString();

    if (Name.isEmpty()) {
        valid = false;
        textInputLayoutName.setError("Please enter valid name!");
    }
    else {
        valid = true;
        textInputLayoutName.setError(null);
    }

    if (Desc.isEmpty()) {
        valid = false;
        textInputLayoutDesc.setError("Please enter valid description!");
    }
    else {
        valid = true;
        textInputLayoutDesc.setError(null);
    }
    return valid;
}
}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    tools:context="com.mtag.app.muaythaiathletesguide.UserPortalActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayoutName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/editTextName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/name"
            android:background="@color/colorAccent" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayoutDesc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/editTextDesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/description"
            android:background="@color/colorAccent" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/buttonPortal"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:background="@color/colorButtons"
        android:onClick="onUserPortalActivity"
        android:text="@string/store_input_button" />

    <TextView
        android:id="@+id/textViewPortal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:textColor="@color/colorAccent" />

</LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

您需要实例化TextView对象才能使用它。例如

    echo '{{Form::text(\'price\','.$f.' , [\'class\' => \'form-control\', \'placeholder\' => \'Price\'])}}';