如何验证 ImageView 是否为空?
How to validate if the ImageView is null?
这是我为我的 sqlite 添加数据的代码,其余的工作正常但是当我没有选择要在 ImageView 中显示的图像然后单击 btnAddData 应用程序崩溃但是当我 select 图像系统在 SQL 中成功插入数据。你认为我需要在我的 IF ELSE 语句中添加什么来制作 Toast "Please Select Signature"
private void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
String time_out = edittime_out.getText().toString();
String time_ino = edittime_in.getText().toString();
String time_cuo = edittime_cu.getText().toString();
String time_bro = edittime_br.getText().toString();
String time_mao = edittime_ma.getText().toString();
String time_tec = edittimei_te.getText().toString();
String time_sta = edittimei_st.getText().toString();
String time_sre = edittimei_sr.getText().toString();
String time_qou = edittimei_qt.getText().toString();
String serial_out = jobserialout.getText().toString();
String timein_jobno = timeinjob.getText().toString();
String time_co = timecompany.getText().toString();
String time_dates = timedates.getText().toString();
String timei_legend = timeilegend.getSelectedItem().toString();
String timei_department = timedept.getText().toString();
Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
if (TextUtils.isEmpty(time_out)) {
Toast.makeText(FormTimeout.this, "Please enter Time Out ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_ino)) {
Toast.makeText(FormTimeout.this, "Please enter Time In ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_cuo)) {
Toast.makeText(FormTimeout.this, "Please enter Customer ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_bro)) {
Toast.makeText(FormTimeout.this, "Please enter Branch ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_mao)) {
Toast.makeText(FormTimeout.this, "Please enter Machine ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_tec)) {
Toast.makeText(FormTimeout.this, "Please enter Technician ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sta)) {
Toast.makeText(FormTimeout.this, "Please enter Status ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sre)) {
Toast.makeText(FormTimeout.this, "Please enter SR",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_qou)) {
Toast.makeText(FormTimeout.this, "Please enter Qoutation ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(serial_out)) {
Toast.makeText(FormTimeout.this, "Please enter Serial ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timein_jobno)) {
Toast.makeText(FormTimeout.this, "Please enter Time in ID ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_legend)) {
Toast.makeText(FormTimeout.this, "Please select Remarks ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_department)) {
Toast.makeText(FormTimeout.this, "Please select Department ",
Toast.LENGTH_LONG).show();
return;
}
boolean isInserted = myDb.insertDataout( time_out, time_ino,
time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou,
serial_out,
timein_jobno, timei_legend, time_co, time_dates, timei_department,
Utils.getBytes(bitmap));
if (isInserted == true) {
Toast.makeText(FormTimeout.this, "Time Out Successfully",
Toast.LENGTH_LONG).show();
edittime_out.setText("");
edittime_in.setText("");
edittime_cu.setText("");
edittime_br.setText("");
edittime_ma.setText("");
edittimei_te.setText("");
edittimei_st.setText("");
edittimei_sr.setText("");
edittimei_qt.setText("");
jobserialout.setText("");
timeinjob.setText("");
Intent intent = new Intent(FormTimeout.this,
Timeoutsms.class);
startActivity(intent);
} else
Toast.makeText(FormTimeout.this, "Time Out Failed",
Toast.LENGTH_LONG).show();
}
});
}
activity_form_timeout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FormTimeout"
android:background="@color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/trans_white_rectangle">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time Out Form"
android:textColor="#ffff"
android:textSize="40sp"
android:textStyle="bold"
android:layout_marginBottom="5sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingTop="15dp"
android:background="@drawable/backhead"
android:drawableRight="@drawable/seervice3"
/>
<EditText
android:id="@+id/editText_idin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:inputType="number"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:paddingRight="10dp"
android:paddingLeft="20dp"
android:background="@drawable/rounded_white"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:hint="Job No."
android:textSize="19dp"
/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="1">
<Button
android:id="@+id/button_view"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_below="@id/editText_idin"
android:text="SELECT ID"
android:background="@drawable/custom_button3"
android:textColor="@color/white"
/>
<Button
android:id="@+id/button_viewAll"
android:layout_width="150dp"
android:layout_marginRight="20dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:text="TIME IN LIST"
android:background="@drawable/custom_button3"
android:textColor="@color/white"/>
</GridLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_margin="10dp"
android:background="@color/black" />
<Button
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="Generate current date and time"
android:drawableLeft="@drawable/clock4"
android:drawableRight="@drawable/calendar"
android:background="@drawable/custom_button3"
android:textColor="@color/white"/>
<TextView
android:id="@+id/editText_timein"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/timein"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:hint="Time In"/>
<TextView
android:id="@+id/editText_timei_out"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/out3"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:hint="Time Out"
/>
<TextView
android:id="@+id/editText_timei_cu"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Customer"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textColor="@color/black"
android:drawableLeft="@drawable/cust"
android:textSize="20dp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp" />
<TextView
android:id="@+id/editText_timei_br"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Branch"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/branch"
android:textColor="@color/black"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp" />
<TextView
android:id="@+id/editText_timei_ma"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Machine"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/machine"
android:textColor="@color/black"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/editText_timei_te"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Technician"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/tech2"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<EditText
android:id="@+id/editText_timei_st"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Status"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/status"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<EditText
android:id="@+id/editText_timei_sr"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="SR No."
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/sr"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<EditText
android:id="@+id/editText_timei_qt"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Qoutation"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/quote"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/legend"
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
<ImageView
android:layout_above="@+id/layout_button"
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/trans_white_rectangle"/>
<LinearLayout
android:id="@+id/layout_button"
android:layout_alignParentBottom="true"
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/signatures"
android:text="SIGN"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/select_image"
android:text="SELECT SIGNATURE"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<TextView
android:id="@+id/textViewr"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:hint="Serial"
android:visibility="invisible"/>
<TextView
android:id="@+id/textViewJobIn"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/editText_timei_co"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:text="JCG Marketing Group Inc."
android:visibility="invisible"/>
<TextView
android:id="@+id/editText_timei_timedate"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/department"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:text="Sevice Department"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:visibility="invisible"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="20dp"
android:rowCount="1">
<Button
android:id="@+id/button_addout"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/custom_button3"
android:text="SAVE"
android:textColor="#ffffff"
android:drawableLeft="@drawable/save"
android:paddingLeft="20dp"/>
</GridLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
用于添加数据的数据库助手代码。
public boolean insertDataout(String timei_out, String timei_in, String
timei_cu, String timei_br, String timei_ma, String timei_te, String timei_st,
String timei_sr, String timei_qt, String serial_out, String timein_jobno,
String
timei_legend, String timei_company, String timei_date, String timei_dept,
byte[]
signature) throws SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2b, timei_out);
contentValues.put(COL_3b, timei_in);
contentValues.put(COL_4b, timei_cu);
contentValues.put(COL_5b, timei_br);
contentValues.put(COL_6b, timei_ma);
contentValues.put(COL_7b, timei_te);
contentValues.put(COL_8b, timei_st);
contentValues.put(COL_9b, timei_sr);
contentValues.put(COL_10b, timei_qt);
contentValues.put(COL_11b, serial_out);
contentValues.put(COL_12b, timein_jobno);
contentValues.put(COL_13b, timei_legend);
contentValues.put(COL_14b, timei_company);
contentValues.put(COL_15b, timei_date);
contentValues.put(COL_16b, timei_dept);
contentValues.put(COL_17b, signature);
long result = db.insert(TABLE_NAME_OUT , null, contentValues);
if (result == -1)
return false;
else
return true;
}
这是Logcat中的错误
LOGCAT错误
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap
android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
第一次改动
if (imageViews != null) {
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
}
也改,
if (imageViews != null && imageViews.getDrawable() == null){
Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
return;
}
您尚未启用此 属性 到 imageView
以从中获取可绘制对象。
imageView.buildDrawingCache();
然后从中得到bitmap
。
final Bitmap bitmap = ((BitmapDrawable)
imageViews.getDrawable()).getBitmap();
if(bitmap==null){
//show toast here
}
方案二:-
全局声明这个变量
Uri pickedImage;
然后点击按钮 btnAddData.setOnClickListener
添加这个条件
if(pickedImage==null ){
// condition where image is not selected
}
像这样
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageViews = (ImageView) findViewById(R.id.image_view);
String time_out = edittime_out.getText().toString();
String time_ino = edittime_in.getText().toString();
String time_cuo = edittime_cu.getText().toString();
String time_bro = edittime_br.getText().toString();
String time_mao = edittime_ma.getText().toString();
String time_tec = edittimei_te.getText().toString();
String time_sta = edittimei_st.getText().toString();
String time_sre = edittimei_sr.getText().toString();
String time_qou = edittimei_qt.getText().toString();
String serial_out = jobserialout.getText().toString();
String timein_jobno = timeinjob.getText().toString();
String time_co = timecompany.getText().toString();
String time_dates = timedates.getText().toString();
String timei_legend = timeilegend.getSelectedItem().toString();
String timei_department = timedept.getText().toString();
if (TextUtils.isEmpty(time_out)) {
Toast.makeText(FormTimeout.this, "Please enter Time Out ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_ino)) {
Toast.makeText(FormTimeout.this, "Please enter Time In ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_cuo)) {
Toast.makeText(FormTimeout.this, "Please enter Customer ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_bro)) {
Toast.makeText(FormTimeout.this, "Please enter Branch ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_mao)) {
Toast.makeText(FormTimeout.this, "Please enter Machine ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_tec)) {
Toast.makeText(FormTimeout.this, "Please enter Technician ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sta)) {
Toast.makeText(FormTimeout.this, "Please enter Status ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sre)) {
Toast.makeText(FormTimeout.this, "Please enter SR", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_qou)) {
Toast.makeText(FormTimeout.this, "Please enter Qoutation ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(serial_out)) {
Toast.makeText(FormTimeout.this, "Please enter Serial ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timein_jobno)) {
Toast.makeText(FormTimeout.this, "Please enter Time in ID ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_legend)) {
Toast.makeText(FormTimeout.this, "Please select Remarks ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_department)) {
Toast.makeText(FormTimeout.this, "Please select Department ", Toast.LENGTH_LONG).show();
return;
}
if(pickedImage==null ){
// condition where image is not selected
Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
return
}
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
boolean isInserted = myDb.insertDataout( time_out, time_ino, time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou, serial_out, timein_jobno, timei_legend, time_co, time_dates, timei_department, Utils.getBytes(bitmap));
if (isInserted == true) {
Toast.makeText(FormTimeout.this, "Time Out Successfully", Toast.LENGTH_LONG).show();
edittime_out.setText("");
edittime_in.setText("");
edittime_cu.setText("");
edittime_br.setText("");
edittime_ma.setText("");
edittimei_te.setText("");
edittimei_st.setText("");
edittimei_sr.setText("");
edittimei_qt.setText("");
jobserialout.setText("");
timeinjob.setText("");
Intent intent = new Intent(FormTimeout.this, Timeoutsms.class);
startActivity(intent);
} else
Toast.makeText(FormTimeout.this, "Time Out Failed", Toast.LENGTH_LONG).show();
}
});
ImageView
尝试缓存之前的 Uri
。您需要在 loading
一个 new
Uri` 之前有效地重置它,如下所示。
// in your onActivityResult()
imageViews.setImageURI(null);
imageViews.setImageURI(pickedImage);
你必须 initialize
你的 imageViews
在 onCreate()
// write this line in your onCreate()
imageViews = (ImageView) findViewById(R.id.image_view);
并且 从 AddData() 中删除 上面的行,如下所示。
private void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// remove below line and add it to onCreate()
imageViews = (ImageView) findViewById(R.id.image_view);
// your other codes here
............
更新: 修改您的 AddData()
方法,如下所示。
private void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String time_out = edittime_out.getText().toString();
String time_ino = edittime_in.getText().toString();
String time_cuo = edittime_cu.getText().toString();
String time_bro = edittime_br.getText().toString();
String time_mao = edittime_ma.getText().toString();
String time_tec = edittimei_te.getText().toString();
String time_sta = edittimei_st.getText().toString();
String time_sre = edittimei_sr.getText().toString();
String time_qou = edittimei_qt.getText().toString();
String serial_out = jobserialout.getText().toString();
String timein_jobno = timeinjob.getText().toString();
String time_co = timecompany.getText().toString();
String time_dates = timedates.getText().toString();
String timei_legend = timeilegend.getSelectedItem().toString();
String timei_department = timedept.getText().toString();
if (TextUtils.isEmpty(time_out)) {
Toast.makeText(FormTimeout.this, "Please enter Time Out ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_ino)) {
Toast.makeText(FormTimeout.this, "Please enter Time In ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_cuo)) {
Toast.makeText(FormTimeout.this, "Please enter Customer ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_bro)) {
Toast.makeText(FormTimeout.this, "Please enter Branch ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_mao)) {
Toast.makeText(FormTimeout.this, "Please enter Machine ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_tec)) {
Toast.makeText(FormTimeout.this, "Please enter Technician ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sta)) {
Toast.makeText(FormTimeout.this, "Please enter Status ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sre)) {
Toast.makeText(FormTimeout.this, "Please enter SR", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_qou)) {
Toast.makeText(FormTimeout.this, "Please enter Qoutation ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(serial_out)) {
Toast.makeText(FormTimeout.this, "Please enter Serial ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timein_jobno)) {
Toast.makeText(FormTimeout.this, "Please enter Time in ID ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_legend)) {
Toast.makeText(FormTimeout.this, "Please select Remarks ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_department)) {
Toast.makeText(FormTimeout.this, "Please select Department ", Toast.LENGTH_LONG).show();
return;
}
if (imageViews != null && imageViews.getDrawable() == null){
Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
return;
}
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
boolean isInserted = myDb.insertDataout( time_out, time_ino, time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou, serial_out, timein_jobno, timei_legend, time_co, time_dates, timei_department, Utils.getBytes(bitmap));
if (isInserted == true) {
Toast.makeText(FormTimeout.this, "Time Out Successfully", Toast.LENGTH_LONG).show();
edittime_out.setText("");
edittime_in.setText("");
edittime_cu.setText("");
edittime_br.setText("");
edittime_ma.setText("");
edittimei_te.setText("");
edittimei_st.setText("");
edittimei_sr.setText("");
edittimei_qt.setText("");
jobserialout.setText("");
timeinjob.setText("");
Intent intent = new Intent(FormTimeout.this, Timeoutsms.class);
startActivity(intent);
} else
Toast.makeText(FormTimeout.this, "Time Out Failed", Toast.LENGTH_LONG).show();
}
});
}
希望对您有所帮助。
这是我为我的 sqlite 添加数据的代码,其余的工作正常但是当我没有选择要在 ImageView 中显示的图像然后单击 btnAddData 应用程序崩溃但是当我 select 图像系统在 SQL 中成功插入数据。你认为我需要在我的 IF ELSE 语句中添加什么来制作 Toast "Please Select Signature"
private void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
String time_out = edittime_out.getText().toString();
String time_ino = edittime_in.getText().toString();
String time_cuo = edittime_cu.getText().toString();
String time_bro = edittime_br.getText().toString();
String time_mao = edittime_ma.getText().toString();
String time_tec = edittimei_te.getText().toString();
String time_sta = edittimei_st.getText().toString();
String time_sre = edittimei_sr.getText().toString();
String time_qou = edittimei_qt.getText().toString();
String serial_out = jobserialout.getText().toString();
String timein_jobno = timeinjob.getText().toString();
String time_co = timecompany.getText().toString();
String time_dates = timedates.getText().toString();
String timei_legend = timeilegend.getSelectedItem().toString();
String timei_department = timedept.getText().toString();
Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
if (TextUtils.isEmpty(time_out)) {
Toast.makeText(FormTimeout.this, "Please enter Time Out ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_ino)) {
Toast.makeText(FormTimeout.this, "Please enter Time In ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_cuo)) {
Toast.makeText(FormTimeout.this, "Please enter Customer ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_bro)) {
Toast.makeText(FormTimeout.this, "Please enter Branch ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_mao)) {
Toast.makeText(FormTimeout.this, "Please enter Machine ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_tec)) {
Toast.makeText(FormTimeout.this, "Please enter Technician ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sta)) {
Toast.makeText(FormTimeout.this, "Please enter Status ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sre)) {
Toast.makeText(FormTimeout.this, "Please enter SR",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_qou)) {
Toast.makeText(FormTimeout.this, "Please enter Qoutation ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(serial_out)) {
Toast.makeText(FormTimeout.this, "Please enter Serial ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timein_jobno)) {
Toast.makeText(FormTimeout.this, "Please enter Time in ID ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_legend)) {
Toast.makeText(FormTimeout.this, "Please select Remarks ",
Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_department)) {
Toast.makeText(FormTimeout.this, "Please select Department ",
Toast.LENGTH_LONG).show();
return;
}
boolean isInserted = myDb.insertDataout( time_out, time_ino,
time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou,
serial_out,
timein_jobno, timei_legend, time_co, time_dates, timei_department,
Utils.getBytes(bitmap));
if (isInserted == true) {
Toast.makeText(FormTimeout.this, "Time Out Successfully",
Toast.LENGTH_LONG).show();
edittime_out.setText("");
edittime_in.setText("");
edittime_cu.setText("");
edittime_br.setText("");
edittime_ma.setText("");
edittimei_te.setText("");
edittimei_st.setText("");
edittimei_sr.setText("");
edittimei_qt.setText("");
jobserialout.setText("");
timeinjob.setText("");
Intent intent = new Intent(FormTimeout.this,
Timeoutsms.class);
startActivity(intent);
} else
Toast.makeText(FormTimeout.this, "Time Out Failed",
Toast.LENGTH_LONG).show();
}
});
}
activity_form_timeout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FormTimeout"
android:background="@color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/trans_white_rectangle">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time Out Form"
android:textColor="#ffff"
android:textSize="40sp"
android:textStyle="bold"
android:layout_marginBottom="5sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingTop="15dp"
android:background="@drawable/backhead"
android:drawableRight="@drawable/seervice3"
/>
<EditText
android:id="@+id/editText_idin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:inputType="number"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:paddingRight="10dp"
android:paddingLeft="20dp"
android:background="@drawable/rounded_white"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:hint="Job No."
android:textSize="19dp"
/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="1">
<Button
android:id="@+id/button_view"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_below="@id/editText_idin"
android:text="SELECT ID"
android:background="@drawable/custom_button3"
android:textColor="@color/white"
/>
<Button
android:id="@+id/button_viewAll"
android:layout_width="150dp"
android:layout_marginRight="20dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:text="TIME IN LIST"
android:background="@drawable/custom_button3"
android:textColor="@color/white"/>
</GridLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_margin="10dp"
android:background="@color/black" />
<Button
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="Generate current date and time"
android:drawableLeft="@drawable/clock4"
android:drawableRight="@drawable/calendar"
android:background="@drawable/custom_button3"
android:textColor="@color/white"/>
<TextView
android:id="@+id/editText_timein"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/timein"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:hint="Time In"/>
<TextView
android:id="@+id/editText_timei_out"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/out3"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:hint="Time Out"
/>
<TextView
android:id="@+id/editText_timei_cu"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Customer"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textColor="@color/black"
android:drawableLeft="@drawable/cust"
android:textSize="20dp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp" />
<TextView
android:id="@+id/editText_timei_br"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Branch"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/branch"
android:textColor="@color/black"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp" />
<TextView
android:id="@+id/editText_timei_ma"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Machine"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/machine"
android:textColor="@color/black"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp" />
<EditText
android:id="@+id/editText_timei_te"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Technician"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/tech2"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<EditText
android:id="@+id/editText_timei_st"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Status"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/status"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<EditText
android:id="@+id/editText_timei_sr"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="SR No."
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/sr"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<EditText
android:id="@+id/editText_timei_qt"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Qoutation"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableLeft="@drawable/quote"
android:textSize="20sp"
android:drawablePadding="5dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:textColorHint="@drawable/selector2"/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/legend"
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
<ImageView
android:layout_above="@+id/layout_button"
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/trans_white_rectangle"/>
<LinearLayout
android:id="@+id/layout_button"
android:layout_alignParentBottom="true"
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/signatures"
android:text="SIGN"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/select_image"
android:text="SELECT SIGNATURE"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<TextView
android:id="@+id/textViewr"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:hint="Serial"
android:visibility="invisible"/>
<TextView
android:id="@+id/textViewJobIn"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/editText_timei_co"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:text="JCG Marketing Group Inc."
android:visibility="invisible"/>
<TextView
android:id="@+id/editText_timei_timedate"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/department"
android:layout_marginTop="13dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/rounded_white"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:textSize="20sp"
android:textColor="@color/red"
android:text="Sevice Department"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:visibility="invisible"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="20dp"
android:rowCount="1">
<Button
android:id="@+id/button_addout"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/custom_button3"
android:text="SAVE"
android:textColor="#ffffff"
android:drawableLeft="@drawable/save"
android:paddingLeft="20dp"/>
</GridLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
用于添加数据的数据库助手代码。
public boolean insertDataout(String timei_out, String timei_in, String
timei_cu, String timei_br, String timei_ma, String timei_te, String timei_st,
String timei_sr, String timei_qt, String serial_out, String timein_jobno,
String
timei_legend, String timei_company, String timei_date, String timei_dept,
byte[]
signature) throws SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2b, timei_out);
contentValues.put(COL_3b, timei_in);
contentValues.put(COL_4b, timei_cu);
contentValues.put(COL_5b, timei_br);
contentValues.put(COL_6b, timei_ma);
contentValues.put(COL_7b, timei_te);
contentValues.put(COL_8b, timei_st);
contentValues.put(COL_9b, timei_sr);
contentValues.put(COL_10b, timei_qt);
contentValues.put(COL_11b, serial_out);
contentValues.put(COL_12b, timein_jobno);
contentValues.put(COL_13b, timei_legend);
contentValues.put(COL_14b, timei_company);
contentValues.put(COL_15b, timei_date);
contentValues.put(COL_16b, timei_dept);
contentValues.put(COL_17b, signature);
long result = db.insert(TABLE_NAME_OUT , null, contentValues);
if (result == -1)
return false;
else
return true;
}
这是Logcat中的错误 LOGCAT错误
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap
android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
第一次改动
if (imageViews != null) {
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
}
也改,
if (imageViews != null && imageViews.getDrawable() == null){
Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
return;
}
您尚未启用此 属性 到 imageView
以从中获取可绘制对象。
imageView.buildDrawingCache();
然后从中得到bitmap
。
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
if(bitmap==null){
//show toast here
}
方案二:-
全局声明这个变量
Uri pickedImage;
然后点击按钮 btnAddData.setOnClickListener
添加这个条件
if(pickedImage==null ){
// condition where image is not selected
}
像这样
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageViews = (ImageView) findViewById(R.id.image_view);
String time_out = edittime_out.getText().toString();
String time_ino = edittime_in.getText().toString();
String time_cuo = edittime_cu.getText().toString();
String time_bro = edittime_br.getText().toString();
String time_mao = edittime_ma.getText().toString();
String time_tec = edittimei_te.getText().toString();
String time_sta = edittimei_st.getText().toString();
String time_sre = edittimei_sr.getText().toString();
String time_qou = edittimei_qt.getText().toString();
String serial_out = jobserialout.getText().toString();
String timein_jobno = timeinjob.getText().toString();
String time_co = timecompany.getText().toString();
String time_dates = timedates.getText().toString();
String timei_legend = timeilegend.getSelectedItem().toString();
String timei_department = timedept.getText().toString();
if (TextUtils.isEmpty(time_out)) {
Toast.makeText(FormTimeout.this, "Please enter Time Out ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_ino)) {
Toast.makeText(FormTimeout.this, "Please enter Time In ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_cuo)) {
Toast.makeText(FormTimeout.this, "Please enter Customer ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_bro)) {
Toast.makeText(FormTimeout.this, "Please enter Branch ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_mao)) {
Toast.makeText(FormTimeout.this, "Please enter Machine ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_tec)) {
Toast.makeText(FormTimeout.this, "Please enter Technician ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sta)) {
Toast.makeText(FormTimeout.this, "Please enter Status ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sre)) {
Toast.makeText(FormTimeout.this, "Please enter SR", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_qou)) {
Toast.makeText(FormTimeout.this, "Please enter Qoutation ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(serial_out)) {
Toast.makeText(FormTimeout.this, "Please enter Serial ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timein_jobno)) {
Toast.makeText(FormTimeout.this, "Please enter Time in ID ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_legend)) {
Toast.makeText(FormTimeout.this, "Please select Remarks ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_department)) {
Toast.makeText(FormTimeout.this, "Please select Department ", Toast.LENGTH_LONG).show();
return;
}
if(pickedImage==null ){
// condition where image is not selected
Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
return
}
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
boolean isInserted = myDb.insertDataout( time_out, time_ino, time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou, serial_out, timein_jobno, timei_legend, time_co, time_dates, timei_department, Utils.getBytes(bitmap));
if (isInserted == true) {
Toast.makeText(FormTimeout.this, "Time Out Successfully", Toast.LENGTH_LONG).show();
edittime_out.setText("");
edittime_in.setText("");
edittime_cu.setText("");
edittime_br.setText("");
edittime_ma.setText("");
edittimei_te.setText("");
edittimei_st.setText("");
edittimei_sr.setText("");
edittimei_qt.setText("");
jobserialout.setText("");
timeinjob.setText("");
Intent intent = new Intent(FormTimeout.this, Timeoutsms.class);
startActivity(intent);
} else
Toast.makeText(FormTimeout.this, "Time Out Failed", Toast.LENGTH_LONG).show();
}
});
ImageView
尝试缓存之前的 Uri
。您需要在 loading
一个 new
Uri` 之前有效地重置它,如下所示。
// in your onActivityResult()
imageViews.setImageURI(null);
imageViews.setImageURI(pickedImage);
你必须 initialize
你的 imageViews
在 onCreate()
// write this line in your onCreate()
imageViews = (ImageView) findViewById(R.id.image_view);
并且 从 AddData() 中删除 上面的行,如下所示。
private void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// remove below line and add it to onCreate()
imageViews = (ImageView) findViewById(R.id.image_view);
// your other codes here
............
更新: 修改您的 AddData()
方法,如下所示。
private void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String time_out = edittime_out.getText().toString();
String time_ino = edittime_in.getText().toString();
String time_cuo = edittime_cu.getText().toString();
String time_bro = edittime_br.getText().toString();
String time_mao = edittime_ma.getText().toString();
String time_tec = edittimei_te.getText().toString();
String time_sta = edittimei_st.getText().toString();
String time_sre = edittimei_sr.getText().toString();
String time_qou = edittimei_qt.getText().toString();
String serial_out = jobserialout.getText().toString();
String timein_jobno = timeinjob.getText().toString();
String time_co = timecompany.getText().toString();
String time_dates = timedates.getText().toString();
String timei_legend = timeilegend.getSelectedItem().toString();
String timei_department = timedept.getText().toString();
if (TextUtils.isEmpty(time_out)) {
Toast.makeText(FormTimeout.this, "Please enter Time Out ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_ino)) {
Toast.makeText(FormTimeout.this, "Please enter Time In ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_cuo)) {
Toast.makeText(FormTimeout.this, "Please enter Customer ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_bro)) {
Toast.makeText(FormTimeout.this, "Please enter Branch ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_mao)) {
Toast.makeText(FormTimeout.this, "Please enter Machine ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_tec)) {
Toast.makeText(FormTimeout.this, "Please enter Technician ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sta)) {
Toast.makeText(FormTimeout.this, "Please enter Status ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_sre)) {
Toast.makeText(FormTimeout.this, "Please enter SR", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(time_qou)) {
Toast.makeText(FormTimeout.this, "Please enter Qoutation ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(serial_out)) {
Toast.makeText(FormTimeout.this, "Please enter Serial ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timein_jobno)) {
Toast.makeText(FormTimeout.this, "Please enter Time in ID ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_legend)) {
Toast.makeText(FormTimeout.this, "Please select Remarks ", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(timei_department)) {
Toast.makeText(FormTimeout.this, "Please select Department ", Toast.LENGTH_LONG).show();
return;
}
if (imageViews != null && imageViews.getDrawable() == null){
Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
return;
}
final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();
boolean isInserted = myDb.insertDataout( time_out, time_ino, time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou, serial_out, timein_jobno, timei_legend, time_co, time_dates, timei_department, Utils.getBytes(bitmap));
if (isInserted == true) {
Toast.makeText(FormTimeout.this, "Time Out Successfully", Toast.LENGTH_LONG).show();
edittime_out.setText("");
edittime_in.setText("");
edittime_cu.setText("");
edittime_br.setText("");
edittime_ma.setText("");
edittimei_te.setText("");
edittimei_st.setText("");
edittimei_sr.setText("");
edittimei_qt.setText("");
jobserialout.setText("");
timeinjob.setText("");
Intent intent = new Intent(FormTimeout.this, Timeoutsms.class);
startActivity(intent);
} else
Toast.makeText(FormTimeout.this, "Time Out Failed", Toast.LENGTH_LONG).show();
}
});
}
希望对您有所帮助。