我正在尝试在Android应用程序的sqlte中使用DatePicker插入日期。给我Nullpointer异常的同时单击“提交”按钮时,我的应用程序崩溃
我尝试将日期的数据类型从Double更改为Numeric。
package com.example.ameyz;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,DatePickerFragment.DateDialogListener {
private static String STRING_EMPTY = "";
private static final String DIALOG_DATE = "DialogDate";
static Spinner sItemsWorkerbrick1 = null;
private static int selectedWorkerIdbrick1 = 0;
private static boolean isEditWorkerbrick1 = false;
CommonDBHelper dbHelper = null;
ImageView calender;
private EditText dateworkerbrick1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new CommonDBHelper(this);
populateSpinnerWorkerbrick1();
calender = findViewById(R.id.calender);
Button addWorkerbrick1 = (Button) findViewById(R.id.btnAddWorkerBrick1);
calender.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
DatePickerFragment dialog = new DatePickerFragment();
dialog.show(manager, DIALOG_DATE);
}
});
addWorkerbrick1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewWorkerbrick1();
}
});
}
private void addNewWorkerbrick1() {
Brick1WorkerDetails movieDetails = new Brick1WorkerDetails();
EditText workerNameBrick1 = (EditText) findViewById(R.id.txtWorkerNameAddBrick1);
EditText salaryworkerBrick1 = (EditText) findViewById(R.id.txtSalaryAddWorkerBrick1);
EditText loanworkerBrick1 = (EditText) findViewById(R.id.txtLoanAddWorkerbrick1);
EditText paidloanworkerBrick1 = (EditText) findViewById(R.id.txtPaidAddWorkerbrick1);
EditText unpaidloanworkerBrick1 = (EditText) findViewById(R.id.txtUnpaidAddWorkerbrick1);
EditText dateworkerbrick1 = (EditText)findViewById(R.id.txtDateAddWorkerbrick1);
if (!STRING_EMPTY.equals(workerNameBrick1.getText().toString()) &&
!STRING_EMPTY.equals(salaryworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(loanworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(paidloanworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(unpaidloanworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(dateworkerbrick1.getText().toString())) {
movieDetails.setWorkerNameBrick1(workerNameBrick1.getText().toString());
movieDetails.setSalaryworkerBrick1(Integer.parseInt(salaryworkerBrick1.getText().toString()));
movieDetails.setLoanworkerBrick1(Integer.parseInt(loanworkerBrick1.getText().toString()));
movieDetails.setPaidloanworkerBrick1(Integer.parseInt(paidloanworkerBrick1.getText().toString()));
movieDetails.setUnpaidloanworkerBrick1(Integer.parseInt(unpaidloanworkerBrick1.getText().toString()));
movieDetails.setWorkerdate(dateworkerbrick1.getText().toString());
dbHelper.addWorkerbrick1(movieDetails);
populateSpinnerWorkerbrick1();
workerNameBrick1.setText("");
salaryworkerBrick1.setText("");
loanworkerBrick1.setText("");
paidloanworkerBrick1.setText("");
unpaidloanworkerBrick1.setText("");
dateworkerbrick1.setText("");
} else {
Toast.makeText(MainActivity.this, "One or more fields left empty!",
Toast.LENGTH_LONG).show();
}
}
private void populateSpinnerWorkerbrick1() {
List<Brick1WorkerDetails> movieDetailsList = dbHelper.getAllWorkersbrick1();
List movieSpinnerList = new ArrayList<>();
for (int i = 0; i < movieDetailsList.size(); i++) {
Brick1WorkerDetails movieDetailsVO = movieDetailsList.get(i);
WorkerBrick1SpinnerVO movieSpinnerVO = new WorkerBrick1SpinnerVO(movieDetailsVO.getWorkerBrick1Id(),
movieDetailsVO.getWorkerNameBrick1());
movieSpinnerList.add(movieSpinnerVO);
}
if (movieDetailsList.size() == 0) {
LinearLayout displayArea42 = (LinearLayout) findViewById(R.id.displayAreaWorkerBrick1);
displayArea42.setVisibility(LinearLayout.GONE);
LinearLayout editButton42 = (LinearLayout) findViewById(R.id.editButtonWorkerbrick1);
editButton42.setVisibility(LinearLayout.GONE);
LinearLayout editArea42 = (LinearLayout) findViewById(R.id.editAreaWorkerBrick1);
editArea42.setVisibility(LinearLayout.GONE);
LinearLayout movieSpinnerLayout42 = (LinearLayout) findViewById(R.id.movieSpinnerLayoutWorkerBrick1);
movieSpinnerLayout42.setVisibility(LinearLayout.GONE);
} else {
LinearLayout movieSpinnerLayout42 = (LinearLayout) findViewById(R.id.movieSpinnerLayoutWorkerBrick1);
movieSpinnerLayout42.setVisibility(LinearLayout.VISIBLE);
}
ArrayAdapter<String> adapter = new ArrayAdapter(
this, android.R.layout.simple_spinner_item, movieSpinnerList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sItemsWorkerbrick1 = (Spinner) findViewById(R.id.movieSpinnerWorkerBrick1);
sItemsWorkerbrick1.setAdapter(adapter);
sItemsWorkerbrick1.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
WorkerBrick1SpinnerVO movieSpinnerVO = (WorkerBrick1SpinnerVO) sItemsWorkerbrick1.getSelectedItem();
int movieId = movieSpinnerVO.getWorkerIdbrick1();
selectedWorkerIdbrick1 = movieId;
Brick1WorkerDetails movieDetailsVO = dbHelper.getWorkerBrick1(movieId);
TextView txtWorkerNameDisplay42 = (TextView) findViewById(R.id.txtWorkerNameDisplayBrick1);
txtWorkerNameDisplay42.setText(movieDetailsVO.getWorkerNameBrick1());
TextView txtSalaryDisplay42 = (TextView) findViewById(R.id.txtSalaryDisplayWorkerBrick1);
String salarybrick1 = Integer.toString(movieDetailsVO.getSalaryworkerBrick1());
txtSalaryDisplay42.setText(salarybrick1);
TextView txtLoanDisplay42 = (TextView) findViewById(R.id.txtLoanDisplayWorkerBrick1);
String loanbrick1 = Integer.toString(movieDetailsVO.getLoanworkerBrick1());
txtLoanDisplay42.setText(loanbrick1);
TextView txtPaidworkerbrick1 = (TextView) findViewById(R.id.txtPaidDisplayWorkerBrick1);
String paidworkerbrick1 = Integer.toString(movieDetailsVO.getPaidloanworkerBrick1());
txtPaidworkerbrick1.setText(paidworkerbrick1);
TextView txtUnpaidworkerbrick1 = (TextView) findViewById(R.id.txtUnpaidDisplayWorkerBrick1);
String unpaidworkerbrick1 = Integer.toString(movieDetailsVO.getUnpaidloanworkerBrick1());
txtUnpaidworkerbrick1.setText(unpaidworkerbrick1);
TextView txtdateworker = findViewById(R.id.txtDateDisplayWorkerBrick1);
txtdateworker.setText(movieDetailsVO.getWorkerdate());
LinearLayout displayArea42 = (LinearLayout) findViewById(R.id.displayAreaWorkerBrick1);
displayArea42.setVisibility(LinearLayout.VISIBLE);
LinearLayout editButton42 = (LinearLayout) findViewById(R.id.editButtonWorkerbrick1);
editButton42.setVisibility(LinearLayout.VISIBLE);
Button btnEdit42 = (Button) findViewById(R.id.btnEditWorkerBrick1);
btnEdit42.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editWorkerbrick1();
}
});
Button btnDelete42 = (Button) findViewById(R.id.btnDeleteWorkerBrick1);
btnDelete42.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteWorkerbrick1();
}
});
}
public void onNothingSelected(AdapterView<?> parent) {
}
private void editWorkerbrick1() {
Brick1WorkerDetails movieDetailsVO = null;
EditText workerNameBrick1 = (EditText) findViewById(R.id.txtWorkerNameEditBrick1);
EditText salaryworkerBrick1 = (EditText) findViewById(R.id.txtSalaryEditWorkerBrick1);
EditText loanworkerBrick1 = (EditText) findViewById(R.id.txtLoanEditWorkerBrick1);
EditText paidloanworkerBrick1 = (EditText) findViewById(R.id.txtPaidEditWorkerBrick1);
EditText unpaidloanworkerBrick1 = (EditText) findViewById(R.id.txtUnpaidEditWorkerBrick1);
EditText dateworkerbrick1 = (EditText)findViewById(R.id.txtDateEditWorkerBrick1);
if (isEditWorkerbrick1) {
if (!STRING_EMPTY.equals(workerNameBrick1.getText().toString()) &&
!STRING_EMPTY.equals(salaryworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(loanworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(paidloanworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(unpaidloanworkerBrick1.getText().toString()) &&
!STRING_EMPTY.equals(dateworkerbrick1.getText().toString())) {
movieDetailsVO = new Brick1WorkerDetails();
LinearLayout displayArea42 = (LinearLayout) findViewById(R.id.displayAreaWorkerBrick1);
displayArea42.setVisibility(LinearLayout.VISIBLE);
LinearLayout editArea42 = (LinearLayout) findViewById(R.id.editAreaWorkerBrick1);
editArea42.setVisibility(LinearLayout.GONE);
Button btnDelete42 = (Button) findViewById(R.id.btnDeleteWorkerBrick1);
btnDelete42.setVisibility(Button.VISIBLE);
movieDetailsVO.setWorkerBrick1Id(selectedWorkerIdbrick1);
movieDetailsVO.setWorkerNameBrick1(workerNameBrick1.getText().toString());
movieDetailsVO.setSalaryworkerBrick1(Integer.parseInt(salaryworkerBrick1.getText().toString()));
movieDetailsVO.setLoanworkerBrick1(Integer.parseInt(loanworkerBrick1.getText().toString()));
movieDetailsVO.setPaidloanworkerBrick1(Integer.parseInt(paidloanworkerBrick1.getText().toString()));
movieDetailsVO.setUnpaidloanworkerBrick1(Integer.parseInt(unpaidloanworkerBrick1.getText().toString()));
movieDetailsVO.setWorkerdate(dateworkerbrick1.getText().toString());
dbHelper.updateWorkerBrick1(movieDetailsVO);
populateSpinnerWorkerbrick1();
sItemsWorkerbrick1 = (Spinner) findViewById(R.id.movieSpinnerWorkerBrick1);
sItemsWorkerbrick1.setSelection(getSpinnerPositionWorkerbrick1(sItemsWorkerbrick1));
isEditWorkerbrick1 = false;
} else {
Toast.makeText(MainActivity.this, "One or more fields left empty!",
Toast.LENGTH_LONG).show();
}
} else {
movieDetailsVO = dbHelper.getWorkerBrick1(selectedWorkerIdbrick1);
LinearLayout displayArea42 = (LinearLayout) findViewById(R.id.displayAreaWorkerBrick1);
displayArea42.setVisibility(LinearLayout.GONE);
LinearLayout editArea42 = (LinearLayout) findViewById(R.id.editAreaWorkerBrick1);
editArea42.setVisibility(LinearLayout.VISIBLE);
Button btnDelete42 = (Button) findViewById(R.id.btnDeleteWorkerBrick1);
btnDelete42.setVisibility(Button.INVISIBLE);
workerNameBrick1.setText(movieDetailsVO.getWorkerNameBrick1());
salaryworkerBrick1.setText(Integer.toString(movieDetailsVO.getSalaryworkerBrick1()));
loanworkerBrick1.setText(Integer.toString(movieDetailsVO.getLoanworkerBrick1()));
paidloanworkerBrick1.setText(Integer.toString(movieDetailsVO.getPaidloanworkerBrick1()));
unpaidloanworkerBrick1.setText(Integer.toString(movieDetailsVO.getUnpaidloanworkerBrick1()));
dateworkerbrick1.setText(movieDetailsVO.getWorkerdate());
isEditWorkerbrick1 = true;
}
}
private int getSpinnerPositionWorkerbrick1(Spinner spinner) {
Adapter adapter = spinner.getAdapter();
int i = 0;
for (; i < adapter.getCount(); i++) {
WorkerBrick1SpinnerVO movieSpinnerVO = (WorkerBrick1SpinnerVO) adapter.getItem(i);
if (selectedWorkerIdbrick1 == movieSpinnerVO.getWorkerIdbrick1()) {
return i;
}
}
return 0;
}
private void deleteWorkerbrick1() {
dbHelper.deleteWorkerbrick1(selectedWorkerIdbrick1);
populateSpinnerWorkerbrick1();
}
@Override
public void onFinishDialog(Date date) {
dateworkerbrick1.setText(formatDate(date));
}
public String formatDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String workdate = sdf.format(date);
return workdate;
}
}
下面是我使用日期选择器设置日期的课程
DatePickerFragment.java
package com.example.ameyz;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import java.util.Date;
import java.util.GregorianCalendar;
public class DatePickerFragment extends DialogFragment {
private DatePicker datePicker;
public interface DateDialogListener{
void onFinishDialog(Date date);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date,null);
datePicker = (DatePicker) v.findViewById(R.id.dialog_date_date_picker);
return new android.support.v7.app.AlertDialog.Builder(getActivity())
.setView(v)
.setTitle("Date")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
int year = datePicker.getYear();
int mon = datePicker.getMonth();
int day = datePicker.getDayOfMonth();
Date date= new GregorianCalendar(year,mon,day).getTime();
DateDialogListener activity =(DateDialogListener)getActivity();
activity.onFinishDialog(date);
dismiss();
}
})
.create();
}
}
这是DBHelper类,其中存储了表的所有列,包括数据库升级和降级的其他代码。
CommonDBHelper.java
package com.example.ameyz;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class CommonDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "trump";
public static final String WORKER_BRICK1 = "worker_brick1";
public static final String KEY_WORKER_IDBRICK1 = "workerIdbrick1";
public static final String KEY_WORKER_NAMEBRICK1 = "workerNamebrick1";
public static final String KEY_WORKERSALARYBRICK1 = "workerSalarybrick1";
public static final String KEY_WORKERLOANBRICK1 = "workerLoanbrick1";
public static final String KEY_WORKERPAIDBRICK1 = "workerPaidbrick1";
public static final String KEY_WORKERUNPAIDBRICK1 = "workerUnpaidbrick1";
public static final String KEY_WORKERDATEBRICK1 = "workerdatebrick1";
//Create Table query for Workers Brick1
private static final String SQL_CREATE_WORKERBRICK1 =
"CREATE TABLE " + WORKER_BRICK1 + " (" +
KEY_WORKER_IDBRICK1 + " INTEGER PRIMARY KEY, " +
KEY_WORKER_NAMEBRICK1 + " TEXT, " +
KEY_WORKERSALARYBRICK1 + " INTEGER, " +
KEY_WORKERLOANBRICK1 + " INTEGER, " +
KEY_WORKERPAIDBRICK1 + " INTEGER, " +
KEY_WORKERUNPAIDBRICK1 + " INTEGER, " +
KEY_WORKERDATEBRICK1 + " NUMERIC " +
")";
public CommonDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_WORKERBRICK1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + WORKER_BRICK1);
onCreate(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
this.onUpgrade(db, oldVersion, newVersion);
}
// Get all worker for Brick1
public long addWorkerbrick1(Brick1WorkerDetails movie) {
// Gets the data repository in write mode
SQLiteDatabase db = this.getWritableDatabase();
//Create a map having movie details to be inserted
ContentValues movie_details = new ContentValues();
movie_details.put(KEY_WORKER_NAMEBRICK1,movie.getWorkerNameBrick1());
movie_details.put(KEY_WORKERSALARYBRICK1, movie.getSalaryworkerBrick1());
movie_details.put(KEY_WORKERLOANBRICK1, movie.getLoanworkerBrick1());
movie_details.put(KEY_WORKERPAIDBRICK1, movie.getPaidloanworkerBrick1());
movie_details.put(KEY_WORKERUNPAIDBRICK1, movie.getUnpaidloanworkerBrick1());
movie_details.put(KEY_WORKERDATEBRICK1, movie.getWorkerdate());
long newRowId = db.insert(WORKER_BRICK1,null, movie_details);
db.close();
return newRowId;
}
// Get all Workers of Brick1
public List getAllWorkersbrick1() {
List movieDetailsList = new ArrayList();
String selectQuery = "SELECT * FROM " + WORKER_BRICK1
+ " ORDER BY " + KEY_WORKER_IDBRICK1 + " DESC";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//if TABLE has rows
if (cursor.moveToFirst()) {
//Loop through the table rows
do {
Brick1WorkerDetails movieDetails = new Brick1WorkerDetails();
movieDetails.setWorkerBrick1Id(cursor.getInt(0));
movieDetails.setWorkerNameBrick1(cursor.getString(1));
movieDetails.setSalaryworkerBrick1(cursor.getInt(2));
movieDetails.setLoanworkerBrick1(cursor.getInt(3));
movieDetails.setPaidloanworkerBrick1(cursor.getInt(4));
movieDetails.setUnpaidloanworkerBrick1(cursor.getInt(5));
movieDetails.setWorkerdate(cursor.getString(cursor.getColumnIndex(CommonDBHelper.KEY_WORKERDATEBRICK1)));
//Add Workers details to list
movieDetailsList.add(movieDetails);
} while (cursor.moveToNext());
}
db.close();
return movieDetailsList;
}
public Brick1WorkerDetails getWorkerBrick1(int movie_id) {
Brick1WorkerDetails movieDetails = new Brick1WorkerDetails();
SQLiteDatabase db = this.getReadableDatabase();
//specify the columns to be fetched
String[] columns = {KEY_WORKER_IDBRICK1, KEY_WORKER_NAMEBRICK1, KEY_WORKERSALARYBRICK1, KEY_WORKERLOANBRICK1, KEY_WORKERPAIDBRICK1, KEY_WORKERUNPAIDBRICK1, KEY_WORKERDATEBRICK1};
//Select condition
String selection = KEY_WORKER_IDBRICK1 + " = ?";
//Arguments for selection
String[] selectionArgs = {String.valueOf(movie_id)};
Cursor cursor = db.query(WORKER_BRICK1, columns, selection,
selectionArgs, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
movieDetails.setWorkerBrick1Id(cursor.getInt(0));
movieDetails.setWorkerNameBrick1(cursor.getString(1));
movieDetails.setSalaryworkerBrick1(cursor.getInt(2));
movieDetails.setLoanworkerBrick1(cursor.getInt(3));
movieDetails.setPaidloanworkerBrick1(cursor.getInt(4));
movieDetails.setUnpaidloanworkerBrick1(cursor.getInt(5));
movieDetails.setWorkerdate(cursor.getString(6));
}
db.close();
return movieDetails;
}
public void updateWorkerBrick1(Brick1WorkerDetails movie) {
SQLiteDatabase db = this.getWritableDatabase();
String workerIds[] = {String.valueOf(movie.getWorkerBrick1Id())};
ContentValues movie_details = new ContentValues();
movie_details.put(KEY_WORKER_NAMEBRICK1, movie.getWorkerNameBrick1());
movie_details.put(KEY_WORKERSALARYBRICK1, movie.getSalaryworkerBrick1());
movie_details.put(KEY_WORKERLOANBRICK1, movie.getLoanworkerBrick1());
movie_details.put(KEY_WORKERPAIDBRICK1, movie.getPaidloanworkerBrick1());
movie_details.put(KEY_WORKERUNPAIDBRICK1, movie.getUnpaidloanworkerBrick1());
movie_details.put(KEY_WORKERDATEBRICK1,movie.getWorkerdate());
db.update(WORKER_BRICK1, movie_details, KEY_WORKER_IDBRICK1 + " = ?", workerIds);
db.close();
}
public void deleteWorkerbrick1(int movieId) {
String workerIds[] = {String.valueOf(movieId)};
SQLiteDatabase db = this.getWritableDatabase();
db.delete(WORKER_BRICK1, KEY_WORKER_IDBRICK1 + " = ?", workerIds);
db.close();
}
}
Logcat错误
Process: com.example.ameyz, PID: 29145
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.example.ameyz.MainActivity.onFinishDialog(MainActivity.java:281)
at com.example.ameyz.DatePickerFragment$1.onClick(DatePickerFragment.java:41)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
答案 0 :(得分:3)
您的dateworkerbrick1
字段为空,因为您从未为其分配任何值:
private EditText dateworkerbrick1;
在对其调用方法之前,将其初始化为非null值。
在另外两种方法中,您有一个具有相同名称的局部变量,这可能会引起一些混乱:
EditText dateworkerbrick1 = (EditText)findViewById(R.id.txtDateAddWorkerbrick1);
这也给您提示如何为字段分配值。