我已经使这个函数比较日期但它只在第二次它给出一个java空指针异常时工作一次,我该如何解决这个问题。我见过很多例子,看看它是否有可能以不同的方式做但我没找到任何东西
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date1 = sdf.parse(datateste);
java.util.Date date2 = sdf.parse(data);
java.util.Date date3 = sdf.parse(data2);
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)<0 || date1.compareTo(date3)>0){
x=1;
}else{
System.out.pri
}catch(ParseException ex){
ex.printStackTrace();
}
if(checkEditText(inputdtestab) )
{
inputdtestab.setError("yipikaei");
btnCreateProduct.setVisibility(View.INVISIBLE);
}else if(x==1){
inputdata.setError("asa");
btnCreateProduct.setVisibility(View.INVISIBLE);
}
else if(checkEditText(quantidade)){
quantidade.setError("mudfukkerrr");
btnCreateProduct.setVisibility(View.INVISIBLE);
}
else{
// ==========================================
// make button visible.
// use the method "setVisibility", not "setViewVisibility"
// and "VISIBLE" in "View.VISIBLE" is in full cap:
btnCreateProduct.setVisibility(View.VISIBLE);
}
}
logcat
06-15 01:26:37.743: E/AndroidRuntime(886): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ccc/pesagem.Newlin_ProductActivity}: java.lang.NullPointerException
06-15 01:26:37.743: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-15 01:26:37.743: E/AndroidRuntime(886): at dalvik.system.NativeStart.main(Native Method)
06-15 01:26:37.743: E/AndroidRuntime(886): Caused by: java.lang.NullPointerException
06-15 01:26:37.743: E/AndroidRuntime(886): at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1001)
06-15 01:26:37.743: E/AndroidRuntime(886): at java.text.DateFormat.parse(DateFormat.java:620)
06-15 01:26:37.743: E/AndroidRuntime(886): at pesagem.Newlin_ProductActivity.restricoes(Newlin_ProductActivity.java:246)
06-15 01:26:37.743: E/AndroidRuntime(886): at pesagem.Newlin_ProductActivity$1.afterTextChanged(Newlin_ProductActivity.java:119)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:3703)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:3554)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.widget.EditText.setText(EditText.java:80)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:3529)
06-15 01:26:37.743: E/AndroidRuntime(886): at pesagem.Newlin_ProductActivity.updateDateDisplay(Newlin_ProductActivity.java:308)
06-15 01:26:37.743: E/AndroidRuntime(886): at pesagem.Newlin_ProductActivity.onCreate(Newlin_ProductActivity.java:213)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.app.Activity.performCreate(Activity.java:5104)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-15 01:26:37.743: E/AndroidRuntime(886): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-15 01:26:37.743: E/AndroidRuntime(886): ... 11 more
所有代码
private TextWatcher myTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {}
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {}
public void afterTextChanged(Editable editable) {
// invoke the following method here:
restricoes();
}
};
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_linha);
//Download JSON file AsyncTask
new DownloadJSON().execute();
Intent i = getIntent();
id2 = i.getStringExtra(TAG_ID);
data = i.getStringExtra(DATA);
data2 = i.getStringExtra(DATA2);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00:00"));
java.util.Date currentLocalTime = cal.getTime();
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
//you can get seconds by adding "...:ss" to it
time.setTimeZone(TimeZone.getTimeZone("GMT+1:00:00"));
String hr = time.format(currentLocalTime);
/** WebServices */
inputdtestab= (EditText) findViewById(R.id.editestab);
inputdata = (EditText) findViewById(R.id.editdata);
inputhora = (EditText) findViewById(R.id.edithora);
quantidade = (EditText) findViewById(R.id.editquantidade);
inputhora.setText(hr);
inputdtestab.addTextChangedListener(myTextWatcher);
quantidade.addTextChangedListener(myTextWatcher);
inputdata.addTextChangedListener(myTextWatcher);
/ / Create button
Button btnCreateProduct = (Button) findViewById(R.id.button2);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
change_date=(Button)findViewById(R.id.button1);
change_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//triggers the DatePickerDialog
showDialog(Date_Dialog_ID);
}
});
//getting current date
cDate=Calendar.getInstance();
cDay=cDate.get(Calendar.DAY_OF_MONTH);
cMonth=cDate.get(Calendar.MONTH);
cYear=cDate.get(Calendar.YEAR);
//assigning the edittext with the current date in the beginning
sDay=cDay;
sMonth=cMonth;
sYear=cYear;
updateDateDisplay(sYear,sMonth,sDay);
restricoes();
}
//restriçoes ver
private boolean checkEditText(EditText edit) {
return edit.getText().length() == 0;
}
public void restricoes() {
int x=0;
Button btnCreateProduct = (Button) findViewById(R.id.button2);
datateste= inputdata.getText().toString();
ntln("How to get here?");
}