EditText始终返回Null - Java NullPointerException

时间:2016-03-04 04:17:04

标签: java android sqlite

美好的一天,我遇到了EditText的问题。我正在学习关于SQLite的教程 - tutorial。这就像我的EditText没有检测到任何值。我甚至试着检查它们是否是空的。这是我的示例代码:

AddItemsActivity.java

public class AddItemsActivity extends AppCompatActivity implements View.OnClickListener{


SQLiteLocalDatabase sqLiteLocalDatabase;

    EditText editTextFullName;
    EditText editTextAddress;
    EditText editTextEmailAdd;
    EditText editTextPassword;
    EditText editTextIDNO;
    Button btnSave;
    Button btnUpdate;
    Button btnSearch;
    Button btnDelete;

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

    sqLiteLocalDatabase = new SQLiteLocalDatabase(AddItemsActivity.this);

    //CALL FUNCTION
    setUpWidgets();
}

public void setUpWidgets(){
    editTextFullName = (EditText) findViewById(R.id.editTextFullName);
    editTextAddress = (EditText) findViewById(R.id.Location);
    editTextEmailAdd = (EditText) findViewById(R.id.editTextEmailAddress);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);
    editTextIDNO = (EditText) findViewById(R.id.editTextID);


    btnSave = (Button) findViewById(R.id.buttonAddContacts);
    btnSave.setOnClickListener(this);
    btnUpdate = (Button) findViewById(R.id.buttonUpdate);
    btnUpdate.setOnClickListener(this);
    btnSearch = (Button) findViewById(R.id.buttonSearch);
    btnSearch.setOnClickListener(this);
    btnDelete = (Button) findViewById(R.id.buttonDelete);
    btnDelete.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    int id = Integer.parseInt(editTextIDNO.getText().toString().trim());
    String fullName = editTextFullName.getText().toString().trim();
    String location = editTextAddress.getText().toString().trim();
    String emailAdd = editTextEmailAdd.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();


    switch (v.getId()){
        case R.id.buttonAddContacts:

            //IF result == -1
            long result = sqLiteLocalDatabase.insert(id,fullName,location,emailAdd,password);

            if(result == -1){
                Toast.makeText(AddItemsActivity.this, "Error",Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(AddItemsActivity.this, "Sucess Id:"+result,Toast.LENGTH_LONG).show();
            }

            break;

        case R.id.buttonUpdate:

            long update = sqLiteLocalDatabase.update(Integer.parseInt(getValue(editTextIDNO)),
                    getValue(editTextFullName),
                    getValue(editTextAddress),
                    getValue(editTextEmailAdd),
                    getValue(editTextPassword)
            );
            if(update == 0){
                Toast.makeText(AddItemsActivity.this, "Error Updating",Toast.LENGTH_LONG).show();
            }else
                if(update == -1){
                    Toast.makeText(AddItemsActivity.this, "Updating",Toast.LENGTH_LONG).show();
                }else {
                Toast.makeText(AddItemsActivity.this, "Error All data updated Id:"+update,Toast.LENGTH_LONG).show();
            }
            break;

        case R.id.buttonSearch:
            break;

        case R.id.buttonDelete:

            long delete = sqLiteLocalDatabase.delete(Integer.parseInt(getValue(editTextIDNO)));
            if(delete == 0) {
                Toast.makeText(AddItemsActivity.this, "Error Delete", Toast.LENGTH_LONG).show();
            } else
            {
                Toast.makeText(AddItemsActivity.this, "Success Delete", Toast.LENGTH_LONG).show();

            }
            break;


    }
}

public String getValue(EditText editText){
    return  editText.getText().toString().trim();

}

@Override
protected void onStart() {
    super.onStart();
    sqLiteLocalDatabase.setUpDb();

}

@Override
protected void onStop() {
    super.onStop();
    sqLiteLocalDatabase.closeTransactionDb();
}

}

对于数据库: SQLiteLocalDatabase .java

public class SQLiteLocalDatabase extends SQLiteOpenHelper{


private SQLiteDatabase sqLiteDatabase;
private static final String DB_NAME = "project.db";
private static final int VERSION = 1;


public static final String DB_TABLE = " user ";
public static final String ID = " _id ";
public static final String FULL_NAME = " fullname ";
public static final String LOCATION = " location ";
public static final String EMAIL_ADD = " email ";
public static final String PASSWORD = " password ";


public SQLiteLocalDatabase(Context context) {
    super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String queryTable = " CREATE TABLE " + DB_TABLE + "( " + ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "+ FULL_NAME + " TEXT NOT NULL, " + LOCATION + " TEXT NOT NULL, " + EMAIL_ADD + " TEXT NOT NULL, " + PASSWORD + " TEXT NOT NULL" + " ) ";

    db.execSQL(queryTable);
}

public void setUpDb(){
    //TO OPEN DATABASE - RE-WRITABLE
    sqLiteDatabase  = getWritableDatabase();

}

public void closeTransactionDb(){
    //CLOSE DB IF OPEN
    if(sqLiteDatabase != null && sqLiteDatabase.isOpen()){
        sqLiteDatabase.close();
    }
}

//INSERT DATA
public long insert(int id,String fullname, String location,String email,String password){

    //CONTENT VALUE contains name-value-pairs
    ContentValues values = new ContentValues();

    if(id != -1) {
        values.put(ID,id);
        values.put(FULL_NAME, fullname);
        values.put(LOCATION, location);
        values.put(EMAIL_ADD, email);
        values.put(PASSWORD, password);
    }
    //Object Table, column, values
    return sqLiteDatabase.insert(DB_NAME, null, values);


}

//UPDATE
public long update(int id, String fullname,String location,String email, String password){
    //CONTENT VALUE contains name-value-pairs
    ContentValues values = new ContentValues();
    values.put(FULL_NAME,fullname);
    values.put(LOCATION,location);
    values.put(EMAIL_ADD,email);
    values.put(PASSWORD,password);

    //WHERE
    String where = ID + " = " +id;

    //Object Table, values, destination-id
    return sqLiteDatabase.update(DB_NAME, values, where, null);
}

//DELETE
//
public long delete(int id){
    //WHERE
    String where = ID + " = " +id;

    //Object Table, values, destination-id
    return sqLiteDatabase.delete(DB_NAME, where, null);
}

public Cursor getAllRecords(){

    String queryDB = "SELECT * FROM " + DB_TABLE;

    return sqLiteDatabase.rawQuery(queryDB,null);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

logcat的:

LASTEST LOGCAT AFTER CHANGING THE EDIT TEXT ID's

addItems.xml

<RelativeLayout
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:fitsSystemWindows="true"
android:background="@color/bg"
tools:context="project.app.elective.ccs.mobileappproject.activities.AddItemsActivity">

<include android:id="@+id/toolbar_extend"
    layout="@layout/toolbar"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:hint="Full Name"
    android:drawableLeft="@drawable/ic_action_icon"
    android:drawableStart="@drawable/ic_action_icon"
    android:textColorHint="@color/textPrimaryColor"
    android:id="@+id/textViewFullName"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_below="@+id/toolbar_extend"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Location Address"
    android:drawableLeft="@drawable/ic_action_icon"
    android:drawableStart="@drawable/ic_action_icon"
    android:textColorHint="@color/textPrimaryColor"
    android:id="@+id/editTextLocation"
    android:layout_below="@+id/textViewFullName"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColorHint="@color/textPrimaryColor"
    android:drawableLeft="@drawable/ic_action_icon"
    android:drawableStart="@drawable/ic_action_icon"
    android:inputType="text"
    android:hint="Email Address"
    android:id="@+id/editTextEmailAddress"
    android:layout_below="@+id/editTextLocation"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_action_icon"
    android:drawableStart="@drawable/ic_action_icon"
    android:textColorHint="@color/textPrimaryColor"
    android:hint="Password"
    android:inputType="textPassword"
    android:id="@+id/editTextPassword"
    android:layout_below="@+id/editTextEmailAddress"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/activity_vertical_margin"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:textColor="@color/textPrimaryColor"
        android:id="@+id/buttonAddContacts"
        android:layout_marginRight="3dp"
        android:layout_marginEnd="3dp"
        style="?attr/borderlessButtonStyle"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:textColor="@color/textPrimaryColor"
        android:id="@+id/buttonUpdate"
        android:layout_below="@+id/buttonAddContacts"
        android:layout_marginRight="3dp"
        android:layout_marginEnd="3dp"
        style="?attr/borderlessButtonStyle"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:textColor="@color/textPrimaryColor"
        android:id="@+id/buttonSearch"
        android:layout_below="@+id/buttonDelete"
        android:layout_marginRight="3dp"
        android:layout_marginEnd="3dp"
        style="?attr/borderlessButtonStyle"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:textColor="@color/textPrimaryColor"
        android:id="@+id/buttonDelete"
        android:layout_below="@+id/buttonUpdate"
        style="?attr/borderlessButtonStyle"/>

</LinearLayout>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_action_icon"
    android:hint="ID no, you can leave it!"
    android:inputType="text"
    android:textColorHint="@color/textPrimaryColor"
    android:ems="10"
    android:id="@+id/editTextID"
    android:layout_below="@+id/editTextPassword"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

layout

6 个答案:

答案 0 :(得分:1)

您不能将id“Location”赋予editText。

 editTextAddress = (EditText) findViewById(R.id.Location);

如果你愿意,可以将它更改为“location”之类的其他内容。

修改

 editTextFullName = (EditText) findViewById(R.id.textViewFullName);
    editTextAddress = (EditText) findViewById(R.id.editTextLocation);

答案 1 :(得分:0)

问题:就像我的EditText没有检测到任何值。

您提供了错误的ID亲爱的..您需要将您的java editext ID从editTextFullName更改为textViewFullName

更改此声明

editTextFullName = (EditText) findViewById(R.id.editTextFullName);

editTextFullName = (EditText) findViewById(R.id.textViewFullName);

答案 2 :(得分:0)

logcat在下面的方法(即onclick)中以fullName字符串设置语句显示NullPointerException,因此在应用toString()和trim()方法之前,请确保editTextFullName textview不为null(具有某些值)。 我怀疑选择查询不能正确返回所有字段值。请使用一些sqlite客户端工具单独尝试查询,以检查选择查询的实际结果。

public void onClick(查看v){

int id = Integer.parseInt(editTextIDNO.getText().toString().trim());

***** String fullName = editTextFullName.getText()。toString()。trim(); *****

String location = editTextAddress.getText().toString().trim();
String emailAdd = editTextEmailAdd.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();

答案 3 :(得分:0)

editTextFullName = (EditText) findViewById(R.id.editTextFullName);更改为editTextFullName = (EditText) findViewById(R.id.textViewFullName);。你指的是错误的ID,这就是它总是为空的原因。

添加

sqLiteDatabase=this.getWritableDatabase();
ContentValues values=new ContentValues();

在插入方法中

答案 4 :(得分:0)

NullPointerException是因为您的EditText全名为null意味着无法找到

我已经知道你的id的EditText代码是“textViewFullName”,你得到id“editTextFullName”

只要你改变

editTextFullName = (EditText) findViewById(R.id.editTextFullName);

editTextFullName = (EditText) findViewById(R.id.textViewFullName);

------------- OR ----------------------

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:hint="Full Name"
    android:drawableLeft="@drawable/ic_action_icon"
    android:drawableStart="@drawable/ic_action_icon"
    android:textColorHint="@color/textPrimaryColor"
    android:id="@+id/textViewFullName"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_below="@+id/toolbar_extend"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:hint="Full Name"
    android:drawableLeft="@drawable/ic_action_icon"
    android:drawableStart="@drawable/ic_action_icon"
    android:textColorHint="@color/textPrimaryColor"
    android:id="@+id/editTextFullName"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_below="@+id/toolbar_extend"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

希望这会有所帮助。如果解决你的问题,请接受它。谢谢。

答案 5 :(得分:0)

像其他人说的那样 你给出了textview的错误id,你需要将你的xml editext id从editTextFullName更改为textViewFullName

或更改此声明

editTextFullName = (EditText) findViewById(R.id.textViewFullName);

return sqLiteDatabase.insert(DB_NAME, null, values);

就像在 SQLiteLocalDatabase 中的“插入()”功能中一样,您可能想要更改

 return sqLiteDatabase.insert(DB_TABLE, null, values);

    contains() 

因为你最终可能会遇到 android.database.sqlite.SQLiteException:没有这样的表:project.db