我是初学者Android程序员。我有问题,无法解决。我想将钱包添加到数据库中,但是应用程序给我带来了错误。看来它采用了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);
}
class
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();
}
});
}
更改上面的代码,它将起作用。我想您正在从某些模型类对象钱包中拿出值,但您没有设置它们。
public void AddWallet(String name, String surname, int start_acc, int curenncy){
ContentValues contentValues = new ContentValues();
contentValues.put(Wallets.COLUMNS.name, name);
contentValues.put(Wallets.COLUMNS.surname, surname);
contentValues.put(Wallets.COLUMNS.start_acc, start_acc);
contentValues.put(Wallets.COLUMNS.curenncy,curenncy);
DbHelper.getWritableDatabase().insert(Wallets.NAME_TABLE,null,contentValues);
}