0
我的應用程序崩潰,我得到這個錯誤我已經通過我的code.It遞歸梳理後也說我的表的主鍵列可能不null。我不知道爲什麼,因爲它是PK,它是自動遞增的。無法暫停活動的Android SQLiteConstraintEcxeption列不能爲null(代碼19)
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AccountsDetailsActivity extends ActionBarActivity {
private EditText meterNumber;
private EditText plotNumber;
private EditText Name;
private EditText phone;
private EditText address;
private EditText account;
private Uri userUri;
private ArrayList<String>meters_list = new ArrayList<String>();
private ArrayList<String>plots_list = new ArrayList<String>();
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.insert_user);
// get action bar
android.app.ActionBar actionBar = getActionBar();
// Enabling Up/Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
String[] projection = new String[] { MeterTableDetails.METER_ID,MeterTableDetails.METER_NUMBER,MeterTableDetails.PLOT_NUMBER};
Cursor cursor = getContentResolver().query(UserAccountsContentProvider.CONTENT_URI_METER, projection, null, null,
null);
if (cursor != null) {
cursor.moveToFirst();
String meter = cursor.getString(cursor
.getColumnIndexOrThrow(MeterTableDetails.METER_NUMBER));
String plot = cursor.getString(cursor
.getColumnIndexOrThrow(MeterTableDetails.PLOT_NUMBER));
plots_list.add(plot);
meters_list.add(meter);
cursor.close();
}
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, meters_list);
final ArrayAdapter<String> plotAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, plots_list);
meterNumber = (EditText) findViewById(R.id.insert_meter_number_user);
meterNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
meterNumber.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new AlertDialog.Builder(AccountsDetailsActivity.this)
.setTitle("Select Meter")
.setAdapter(dataAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
meterNumber.setText(meters_list.toString());
dialog.dismiss();
}
}).create().show();
}
});
plotNumber = (EditText) findViewById(R.id.insert_plot_user);
plotNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
plotNumber.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AccountsDetailsActivity.this)
.setTitle("Select Plot Number")
.setAdapter(plotAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
plotNumber.setText(plots_list.toString());
dialog.dismiss();
}
}).create().show();
}
});
Name = (EditText) findViewById(R.id.insert_name_edit);
Name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
address = (EditText) findViewById(R.id.insert_address);
address.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
account = (EditText) findViewById(R.id.insert_accountNumber);
account.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
phone = (EditText) findViewById(R.id.insert_phone);
phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
Button confirmButton = (Button) findViewById(R.id.btn_insert_user);
Bundle extras = getIntent().getExtras();
// check from the saved Instance
userUri = (bundle == null) ? null
: (Uri) bundle
.getParcelable(UserAccountsContentProvider.CONTENT_ITEM_ACCOUNT_TYPE);
// or if passed from other activities
if (extras != null) {
userUri = extras
.getParcelable(UserAccountsContentProvider.CONTENT_ITEM_ACCOUNT_TYPE);
fillData(userUri);
}
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(Name.getText().toString())) {
makeToast();
} else if (TextUtils.isEmpty(meterNumber.getText().toString())) {
makeToast();
} else if (TextUtils.isEmpty(plotNumber.getText().toString())) {
makeToast();
} else if (TextUtils.isEmpty(address.getText().toString())) {
makeToast();
}
else if (TextUtils.isEmpty(account.getText().toString())) {
makeToast();
}else if (TextUtils.isEmpty(phone.getText().toString())) {
makeToast();
}else {
setResult(RESULT_OK);
finish();
}
}
private void makeToast() {
Toast.makeText(AccountsDetailsActivity.this,
"Please enter all fields", Toast.LENGTH_LONG).show();
}
});
}
private void fillData(Uri uri) {
String[] projection = new String[] { AccountsTableDetails.KEY_CUSTOMER_ID,
AccountsTableDetails.KEY_LAST_NAME,
AccountsTableDetails.KEY_METER_NUMBER,
AccountsTableDetails.KEY_PLOT_NUMBER,
AccountsTableDetails.KEY_ADDRESS,
AccountsTableDetails.KEY_PHONE_NUMBER,
AccountsTableDetails.KEY_ACCOUNT_NUMBER};
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
if (cursor != null) {
cursor.moveToFirst();
Name.setText(cursor.getString(cursor
.getColumnIndexOrThrow(AccountsTableDetails.KEY_LAST_NAME)));
meterNumber.setText(cursor.getString(cursor
.getColumnIndexOrThrow(AccountsTableDetails.KEY_METER_NUMBER)));
plotNumber.setText(cursor.getString(cursor
.getColumnIndexOrThrow(AccountsTableDetails.KEY_PLOT_NUMBER)));
address.setText(cursor.getString(cursor
.getColumnIndexOrThrow(AccountsTableDetails.KEY_ADDRESS)));
phone.setText(cursor.getString(cursor
.getColumnIndexOrThrow(AccountsTableDetails.KEY_PHONE_NUMBER)));
account.setText(cursor.getString(cursor
.getColumnIndexOrThrow(AccountsTableDetails.KEY_ACCOUNT_NUMBER)));
// always close the cursor
cursor.close();
}
}
private void hideKeyboard(View v) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putParcelable(
UserAccountsContentProvider.CONTENT_ITEM_ACCOUNT_TYPE,
userUri);
}
private void saveState() {
String name = Name.getText().toString();
String meter = meterNumber.getText().toString();
String plot = plotNumber.getText().toString();
String Address = address.getText().toString();
String Account = account.getText().toString();
String Phone = phone.getText().toString();
// only save if either name or account number
// is available
if (name.length() == 0 && Account.length() == 0) {
return;
}
ContentValues values = new ContentValues();
values.put(AccountsTableDetails.KEY_LAST_NAME, name);
values.put(AccountsTableDetails.KEY_METER_NUMBER, meter);
values.put(AccountsTableDetails.KEY_PLOT_NUMBER, plot);
values.put(AccountsTableDetails.KEY_ADDRESS, Address);
values.put(AccountsTableDetails.KEY_ACCOUNT_NUMBER, Account);
values.put(AccountsTableDetails.KEY_PHONE_NUMBER, Phone);
if (userUri == null) {
// New item
userUri = getContentResolver().insert(
UserAccountsContentProvider.CONTENT_URI_ACCOUNTS,
values);
} else {
// Update meter
getContentResolver().update(userUri, values, null, null);
}
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
}
這是logcat的:
04-01 17:48:30.413: E/AndroidRuntime(2317): java.lang.RuntimeException: Unable to pause activity {com.isys.waterbillingsystem/com.isys.waterbillingsystem.AccountsDetailsActivity}: android.database.sqlite.SQLiteConstraintException: customers.customerMeterId may not be NULL (code 19)
04-01 17:48:30.413: E/AndroidRuntime(2317): Caused by: android.database.sqlite.SQLiteConstraintException: customers.customerMeterId may not be NULL (code 19)
04-01 17:48:30.413: E/AndroidRuntime(2317): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
04-01 17:48:30.413: E/AndroidRuntime(2317): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
04-01 17:48:30.413: E/AndroidRuntime(2317): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
04-01 17:48:30.413: E/AndroidRuntime(2317): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
還有什麼問題呢?
嘗試'Log.d( 「表ID」,MeterTableDetails.METER_ID);'您的查詢之前,看看它輸出。我猜那是你'NULL'並沒有什麼,它實際上是在你的數據庫。你應該得到一個'SQLite的Browser'只是爲了仔細檢查什麼在那裏... – nathansizemore
@nathansizemore感謝you.That幫我調試我code.I終於解決it.Very愚蠢的錯誤 – naffie
請張貼的解決方案,以便其他人誰可能查看此尋找幫助將知道問題的原因:) – nathansizemore