在我的应用程序中,我想通过检查条件来执行操作。我有两个字符串odate和indate。这些字符串从数据库中获取数据如果两个字符串中都没有数据,我想显示一个警告对话框。如果indate或odate中有任何值,我想转到下一个活动。当我试图检查"如果indate和odate等于null"则会出现问题。执行一项功能。但是当它实际上为null时,它会显示一个空指针异常,而不是输入else子句。 这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_iem);
getdate();
invorder();
orderheader();
}
private void orderheader() {
// TODO Auto-generated method stub
try{
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select order_date from order_header where cust_code="+code, null);
while (c.moveToNext()){
odate = c.getString(c.getColumnIndex("order_date"));
Log.v("orderheader", odate);
}}
catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
private void invorder() {
// TODO Auto-generated method stub
try{
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select inv_date from inv_header where cust_code="+code, null);
while (c.moveToNext()){
indate = c.getString(c.getColumnIndex("inv_date"));
Log.v("invheader", indate);
}
catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
public void onBackPressed(){
gettime();
String m = "";
if(odate.matches("") && indate.matches("")){
DatabaseHelper databasecontroller = new DatabaseHelper(Menu_iem.this);
databas.put("rscode","0");
databasecontroller.enterreason(databas);
Intent inn= new Intent(getApplicationContext(),Home.class);
startActivity(inn);
}
else{
spinneralert();
}
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
List<String> labels = db.getAllLabels();
// Creating adapter for spinner
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, labels);
final Spinner sp = new Spinner(Menu_iem.this);
sp.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
sp.setAdapter(dataAdapter);
// attaching data adapter to spinner
String str = "PLEASE SELECT YOUR REASON ";
sp.setAdapter(dataAdapter);
sp.setOnItemSelectedListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(Menu_iem.this);
builder.setTitle(str);
builder.setView(sp);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
DatabaseHelper databasecontroller = new DatabaseHelper(Menu_iem.this);
databas.put("rscode",rc);
databasecontroller.enterreason(databas);
Intent inn= new Intent(getApplicationContext(),Home.class);
startActivity(inn);
}
});
builder.setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
builder.create().show();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String label = parent.getItemAtPosition(position).toString();
try{
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select reason_code from reason_master where reason_name =" +label, null);
while (c.moveToNext()){
rc = c.getString(c.getColumnIndex("reason_code"));
}
}catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
我尝试了其他问题,但没有任何帮助。请帮忙解决这个问题。
EDIT 这是我的错误日志
04-07 16:52:23.584: E/AndroidRuntime(4304): FATAL EXCEPTION: main
04-07 16:52:23.584: E/AndroidRuntime(4304): Process: com.example.sellatease, PID: 4304
04-07 16:52:23.584: E/AndroidRuntime(4304): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.Spinner.measureContentWidth(Spinner.java:759)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.Spinner.onMeasure(Spinner.java:510)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
04-07 16:52:23.584: E/AndroidRuntime(4304): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.View.measure(View.java:17547)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1148)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.Choreographer.doFrame(Choreographer.java:550)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.os.Handler.handleCallback(Handler.java:739)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.os.Looper.loop(Looper.java:135)
04-07 16:52:23.584: E/AndroidRuntime(4304): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-07 16:52:23.584: E/AndroidRuntime(4304): at java.lang.reflect.Method.invoke(Native Method)
04-07 16:52:23.584: E/AndroidRuntime(4304): at java.lang.reflect.Method.invoke(Method.java:372)
04-07 16:52:23.584: E/AndroidRuntime(4304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-07 16:52:23.584: E/AndroidRuntime(4304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
if(odate.matches("") && indate.matches("")){
Here you are matching the string with empty. First make sure those string are not null and then check wether its having any value or not.
答案 1 :(得分:0)
Do
if(!TextUtils.isEmpty(odate) || !TextUtils.isEmpty(indate) {
// go to activity
} else {
// show dialog
}
You can read more about TextUtils http://developer.android.com/reference/android/text/TextUtils.html
答案 2 :(得分:0)
change if(odate.matches("") && indate.matches("")){
to
if((odate == null || odate.trim.length() < 2) && (indate == null || indate.trim.length() < 2)){
ofcourse you can change the number 2 to whatever length you need.
From your error log, I think the error is that the variable 'label' is null in line
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, labels);
Check whether the dbHelper functions returns null to label, and add a checking before using labels like
If(labels !=null){//create adapter here}
答案 3 :(得分:0)
if(odate.matches("") && indate.matches("")){
}
replace your above code with
if(odate!=null && indate!=null){
if(odate.matches("") && indate.matches("")){
}
}else{
// you getting null object
}
it will first check if your string object is null if its not null than after that it will try to invoke mathes function.