【发布时间】:2016-12-11 20:53:48
【问题描述】:
我是一名初级安卓程序员。我有问题,无法解决。我想将钱包添加到数据库中,但应用程序给我一个错误。 似乎它采用了 EditText 的值。怎么了?
我有错误:
Error inserting currency=null surname=null start_acc=0.0 name=null
android.database.sqlite.SQLiteConstraintException: NOT NULL
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dodawanie_portfela"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.xxx.xxx.xxx">
<TextView
android:text="xxx"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="29dp"
android:layout_marginStart="29dp"
android:layout_marginTop="75dp"
android:id="@+id/textView3" />
<TextView
android:text="xxx"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_alignLeft="@+id/textView3"
android:layout_alignStart="@+id/textView3"
android:layout_marginTop="20dp"
android:id="@+id/textView4" />
<TextView
android:text="xxx"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:layout_alignLeft="@+id/textView4"
android:layout_alignStart="@+id/textView4"
android:layout_marginTop="20dp"
android:id="@+id/textView5" />
<TextView
android:text="xxx"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView5"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5"
android:layout_marginTop="20dp"
android:id="@+id/textView6" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:id="@+id/curenncy" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_above="@+id/textView4"
android:layout_alignLeft="@+id/curenncy"
android:layout_alignStart="@+id/curenncy"
android:id="@+id/name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_above="@+id/textView5"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:id="@+id/surname" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_above="@+id/textView6"
android:layout_alignLeft="@+id/curenncy"
android:layout_alignStart="@+id/curenncy"
android:id="@+id/cash" />
<Button
android:text="ADD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addwallet"
android:width="500dp"
android:background="@color/common_action_bar_splitter"
android:elevation="0dp"
android:layout_marginTop="78dp"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
方法
public void AddWallet(String name, String surname, int start_acc, int curenncy){
ContentValues contentValues = new ContentValues();
contentValues.put(Wallets.COLUMNS.name, wallet.getName());
contentValues.put(Wallets.COLUMNS.surname, wallet.getSurname());
contentValues.put(Wallets.COLUMNS.start_acc, wallet.getStart_acc());
contentValues.put(Wallets.COLUMNS.curenncy, wallet.getCurrency());
DbHelper.getWritableDatabase().insert(Wallets.NAME_TABLE,null,contentValues);
}
类
public class Add extends AppCompatActivity {
EditText name;
EditText surname;
EditText start_acc;
Button add;
Spinner curenncy;
int id_value;
DatabaseDEO db = new DatabaseDEO(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
name = (EditText) findViewById(R.id.name);
surname = (EditText) findViewById(R.id.surname);
start_acc = (EditText) findViewById(R.id.cash);
add = (Button) findViewById(R.id.addwallet);
curenncy = (Spinner) findViewById(R.id.curenncy);
curenncy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
id_value = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final ArrayList<String> list= db.getToSpinner();
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,R.layout.currency_spinner, R.id.text,list);
curenncy.setAdapter(adapter);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pname = name.getText().toString();
String psurname = surname.getText().toString();
int pstart_acc = Integer.parseInt(start_acc.getText().toString());
int pid_value = id_value;
db.AddWallet(pname,psurname,pstart_acc,pid_value);
Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_LONG).show();
}
});
}
【问题讨论】:
-
您的一列不接受空值,但您从编辑文本中获得空值。确保您在edittext中输入了一些内容。您还应该在插入数据之前检查 null。
-
是的,一列没有空属性,但我在edittext中写入文本并在微调器中选择选项。