目前在这个问题上停留了2天。
如果我收到错误但是它一直在崩溃,我试图显示警告对话
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Login.this);
我试过了
getApplicationContext()和getBaseContext()也没有帮助。
一些代码:
触发登录的代码
private OnClickListener loginBtnClickListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("APP_STATUS", "Login Button");
//Pass user input to webservice
SoapHandler soapHandler = new SoapHandler();
//if token invalid/expire, go back to login screen.
soapHandler.loginWS(type, loginId, password);
}
};
//******************** Main Webservice Handler
public void callSoapService(final String action, final SoapSerializationEnvelope envelope) {
resultHandler = new ResultHandler();
Thread background = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
Log.d("APP_STATUS", "=====================Sending Webservice================");
androidHttpTransport.call(action, envelope);
Log.d("APP_STATUS", "*************** Response from Webservice ***************");
Object resp = envelope.getResponse();
String res = (resp != null ? resp.toString() : "-1;No response found.");
webservice_result = res;
Log.d("APP_STATUS", webservice_result);
threadMsg(webservice_result);
}
catch (Exception ex){
error = ex.getClass().toString();
Log.d("APP_STATUS", error);
}
}
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = ResultHandler.handler.obtainMessage();
Bundle b = new Bundle();
b.putString(action, msg);
msgObj.setData(b);
ResultHandler.handler.sendMessage(msgObj);
}
else{
//Server unreachable/network issue
Log.d("APP_STATUS", "Server unreachable or network Issue");
}
}
});
//start new thread to send webservice
background.start();
}
最后处理结果的代码
public void handleMessage(Message msg) {
action = SoapHandler.action;
String webservice_result = msg.getData().getString(action);
if ((null != webservice_result)) {
// ALERT MESSAGE
// if Server is online, return result to respective method
//******************************* return result to Login activity
if(action.equals("Login")){
Login login = new Login();
login.isLoginSuccess(webservice_result);
}
}
else
{
// ALERT MESSAGE
//if Server cannot be reached
Log.d("APP_STATUS", "Server down");
}
}
触发警报对话框的地方:
public void isLoginSuccess(String result)
{
if (result.startsWith("0")) {
Log.d("APP_STATUS", "LOGIN OK");
}
else{
AlertDialog alertDialog = new AlertDialog.Builder(
context).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
alertDialog.show();
}
}
错误日志:
09-03 16:47:20.950: E/AndroidRuntime(15934): FATAL EXCEPTION: main
09-03 16:47:20.950: E/AndroidRuntime(15934): java.lang.NullPointerException
09-03 16:47:20.950: E/AndroidRuntime(15934): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
09-03 16:47:20.950: E/AndroidRuntime(15934): at com.example.mym1revamp.Login.isLoginSuccess(Login.java:113)
09-03 16:47:20.950: E/AndroidRuntime(15934): at com.example.mym1revamp.Soap.ResultHandler$1.handleMessage(ResultHandler.java:27)
09-03 16:47:20.950: E/AndroidRuntime(15934): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 16:47:20.950: E/AndroidRuntime(15934): at android.os.Looper.loop(Looper.java:137)
09-03 16:47:20.950: E/AndroidRuntime(15934): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-03 16:47:20.950: E/AndroidRuntime(15934): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 16:47:20.950: E/AndroidRuntime(15934): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 16:47:20.950: E/AndroidRuntime(15934): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-03 16:47:20.950: E/AndroidRuntime(15934): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-03 16:47:20.950: E/AndroidRuntime(15934): at dalvik.system.NativeStart.main(Native Method)
添加了登录类:
public class Login extends Slider {
/*private String ID_TYPE = 0;
private String ID = "";
private String MobileNumber = "";*/
//public final Context context = this;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
getSlidingMenu(this);
buttonControls();
}
public void buttonControls(){
findViewById(R.id.btn_login).setOnClickListener(loginBtnClickListener);
//findViewById(R.id.btn_login).setOnClickListener(dafugBtnClickListener);
}
private OnClickListener dafugBtnClickListener = new OnClickListener() {
public void onClick(View v) {
Log.d("APP_STATUS", "Login Button");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Login.this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Login.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
};
private OnClickListener loginBtnClickListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("APP_STATUS", "Login Button");
//Pass user input to webservice
//String[] items = getResources().getStringArray(R.array.auth_type_title);
//String authType = items[(Integer)((TextView) findViewById(R.id.login_idtype_title_view)).getTag()];
//String authId = ((EditText)findViewById(R.id.login_auth_id)).getText().toString().toUpperCase();
//String mobileNo = ((EditText)findViewById(R.id.login_mobile_no)).getText().toString();
String authType =
String authId =
String mobileNo =
//SoapHandler soapHandler = SoapHandler.getInstance();
SoapHandler soapHandler = new SoapHandler();
//if token invalid/expire, go back to login screen.
soapHandler.loginWS(authType, authId, mobileNo);
}
};
public void isLoginSuccess(String result)
{
Log.d("APP_STATUS", "IS LOGIN SUCCESS?");
if (result.startsWith("0")) {
Log.d("APP_STATUS", "LOGIN OK");
}
else{
Log.d("APP_STATUS", "Login Button");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Login.this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
Login.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
//switch to remember login credentials
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
Log.d("APP_STATUS", "ON");
} else {
Log.d("APP_STATUS", "OFF");
}
}
}
答案 0 :(得分:0)
试试这种方式
public void isLoginSuccess(final String result) {
new Handler().post(new Runnable() {
@Override
public void run() {
if (result.startsWith("0")) {
Log.d("APP_STATUS", "LOGIN OK");
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
alertDialog.show();
}
}
});
}