单击按钮时出现 IllegalStateException - 原因:java.lang.NullPointerException

Getting IllegalStateException on Button Click - caused by: java.lang.NullPointerException

我尝试将数据添加到 sqlite,但是当我单击“添加”按钮时出现错误并且我的应用程序关闭了。不明白,自己找解决方案浪费了这么多时间..

这是我的 java 代码,AddSampleActivity.java

package com.example.mangan;
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;

public class AddSampleActivity extends AppCompatActivity {

DatabaseHelper myDb;
EditText editNama, editJarak, editRating , editHarga;

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

    editNama = (EditText) findViewById(R.id.nama);
    editJarak = (EditText) findViewById(R.id.jarak);
    editRating = (EditText) findViewById(R.id.rating);
    editHarga = (EditText) findViewById(R.id.harga);

    myDb = new DatabaseHelper(this);
}

public void addData(View view) {
    boolean isInserted = myDb.insertData(editNama.getText().toString(),
            editJarak.getText().toString(),
            editRating.getText().toString(),
            editHarga.getText().toString() );
    if(isInserted)
        Toast.makeText(AddSampleActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
    else
        Toast.makeText(AddSampleActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
    }
}

我的布局,activity_add_sample.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.mangan.AddSampleActivity">

<EditText
    android:id="@+id/jarak_editText"
    android:hint="Jarak"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/rating_editText"
    android:hint="Rating"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/harga_editText"
    android:hint="Harga"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/add_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="addData"
    android:text="Add"/>

</LinearLayout>

这是全栈跟踪

java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:5675)
                  at android.view.View$PerformClick.run(View.java:22651)
                  at android.os.Handler.handleCallback(Handler.java:836)
                  at android.os.Handler.dispatchMessage(Handler.java:103)
                  at android.os.Looper.loop(Looper.java:208)
                  at android.app.ActivityThread.main(ActivityThread.java:6267)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5675) 
                  at android.view.View$PerformClick.run(View.java:22651) 
                  at android.os.Handler.handleCallback(Handler.java:836) 
                  at android.os.Handler.dispatchMessage(Handler.java:103) 
                  at android.os.Looper.loop(Looper.java:208) 
                  at android.app.ActivityThread.main(ActivityThread.java:6267) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.example.mangan.AddSampleActivity.addData(AddSampleActivity.java:30)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5675) 
                  at android.view.View$PerformClick.run(View.java:22651) 
                  at android.os.Handler.handleCallback(Handler.java:836) 
                  at android.os.Handler.dispatchMessage(Handler.java:103) 
                  at android.os.Looper.loop(Looper.java:208) 
                  at android.app.ActivityThread.main(ActivityThread.java:6267) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

好像是addData方法上的一些内容有问题。我读了另一个问题,,但问题有点不同..

您的编辑文本 ID 不正确。像下面这样更改它们

editJarak = (EditText) findViewById(R.id.jarak_editText);
editRating = (EditText) findViewById(R.id.rating_editText);
editHarga = (EditText) findViewById(R.id.harga_editText);

您的编辑文本 ID 有误。试试这个,

    editNama = (EditText) findViewById(R.id.nama_editText);
    editJarak = (EditText) findViewById(R.id.jarak_editText);
    editRating = (EditText) findViewById(R.id.rating_editText);
    editHarga = (EditText) findViewById(R.id.harga_editText);