在下面的代码中,按钮savebutton的事件列表器会导致强制关闭错误。
取消按钮有效。
else if(menuItemName=="Make Tenant InActive"){
// TODO Auto-generated method stub
// get tenant id
// inflate the layout
// put id in textbox
// get date dialog to work!
try{
final Dialog dialog = new Dialog(GetTenantList.this);
dialog.setContentView(R.layout.tenant_inactive);
dialog.setTitle("Make Tenant InActive(Left)");
int pos = menuInfo.position;
Tenants t=(Tenants)
tenants.getItem(pos);
Log.e("id", Integer.toOctalString(t.get_id()));
Log.e("Name",t.getFirstName().toString());
int tenantid=t.get_id();
final TextView idtxt =(TextView)
dialog.findViewById(R.id.txtID);
idtxt.setText(Integer.toString(tenantid));
Button savebutton = (Button)
dialog.findViewById(R.id.upadte);
savebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int myid= Integer.parseInt(idtxt.getText().toString());
EditText dateInActive= (EditText)
findViewById(R.id.txtInactive_Date);
java.util.Date inactive = null;
try {
inactive = fm.parse(dateInActive.getText().toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String InActiveDate=fm.format(inactive);
if(InActiveDate.length()==0)
{
CharSequence msg ="Date InActive not entered";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
if(db.IsvalidDate(InActiveDate )==false)
{
CharSequence msg ="Date must be entered in format yyyy-mm-dd";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
try{
db.SetTenantInActive(myid, InActiveDate );
}catch(Exception e)
{
Log.e("Setting InActive Err", e.toString());
}
}
});
Button cancelbutton = (Button)
dialog.findViewById(R.id.back);
cancelbutton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
// TODO Auto-generated method stub
}
});
dialog.show();
}catch(Exception e)
{
Log.e("Bad err", e.toString());
}
}
这是logcat打印。
02-12 18:59:37.470: W/dalvikvm(2624): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-12 18:59:37.491: E/AndroidRuntime(2624): FATAL EXCEPTION: main
02-12 18:59:37.491: E/AndroidRuntime(2624): java.lang.NullPointerException
02-12 18:59:37.491: E/AndroidRuntime(2624): at com.example.metermanager.GetTenantList$1.onClick(GetTenantList.java:162)
02-12 18:59:37.491: E/AndroidRuntime(2624): at android.view.View.performClick(View.java:2485)
02-12 18:59:37.491: E/AndroidRuntime(2624): at android.view.View$PerformClick.run(View.java:9080)
02-12 18:59:37.491: E/AndroidRuntime(2624): at android.os.Handler.handleCallback(Handler.java:587)
02-12 18:59:37.491: E/AndroidRuntime(2624): at android.os.Handler.dispatchMessage(Handler.java:92)
02-12 18:59:37.491: E/AndroidRuntime(2624): at android.os.Looper.loop(Looper.java:123)
02-12 18:59:37.491: E/AndroidRuntime(2624): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 18:59:37.491: E/AndroidRuntime(2624): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:59:37.491: E/AndroidRuntime(2624): at java.lang.reflect.Method.invoke(Method.java:507)
02-12 18:59:37.491: E/AndroidRuntime(2624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 18:59:37.491: E/AndroidRuntime(2624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 18:59:37.491: E/AndroidRuntime(2624): at dalvik.system.NativeStart.main(Native Method)
02-12 18:59:41.510: I/Process(2624): Sending signal. PID: 2624 SIG: 9
代码的其他部分正常运行。 即使是try..catch语句也没有发现错误!
罗纳德 我在click事件上做了如下更改。 这里是编辑
@Override
public void onClick(View v) {
int myid= Integer.parseInt(idtxt.getText().toString());
EditText dateInActive= (EditText)
findViewById(R.id.txtInactive_Date);
try {
Date inactive = fm.parse(dateInActive.getText().toString());
String InActiveDate=fm.format(inactive);
if(InActiveDate.length()==0)
{
CharSequence msg ="Date InActive not entered";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
if(db.IsvalidDate(InActiveDate )==false)
{
CharSequence msg ="Date must be entered in format yyyy-mm-dd";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
db.SetTenantInActive(myid, InActiveDate );
}catch(Exception e)
{
Log.e("Setting InActive Err", e.toString());
}
}