警报对话框导致崩溃

时间:2014-03-08 16:27:55

标签: android alertdialog

代码正常工作,直到我添加了alertdialog代码,请帮助我,我无法找到它崩溃的原因:

public class LoginActivity extends Activity {

private static String TAG = "Login";

private String username;
private String password;
private TextView myTextView;
//new AlertDialog.Builder(this).create().setTitle("Login Successful").setMessage("You're now logged in press ok to continue").setPositiveButton("OK", null);
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);


private static final String SOAP_ACTION = "http://192.168.1.6:9999/POIWebServer/POIWebService/loginUser";
private static final String METHOD_NAME = "loginUser";
private static final String NAMESPACE = "http://192.168.1.6:9999/POIWebServer/POIWebService";
private static final String URL = "http://192.168.1.6:9999/POIWebServer/POIWebService/POIWebService?WSDL";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);    

}

@Override
protected void onResume() {
    super.onResume();
    setContentView(R.layout.activity_login);    


    builder1.setTitle("Login Successful");
    builder1.setMessage("You're now logged in, press ok to continue");
    builder1.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });


    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        myTextView =  (TextView)findViewById(R.id.textView1);

           EditText et1 = (EditText) findViewById(R.id.editText1);
           EditText et2 = (EditText) findViewById(R.id.editText2);

           username = et1.getText().toString();
           password = et2.getText().toString();


           Thread networkThread = new Thread(){
               @Override
               public void run(){
                   try {

                       SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
                       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                       envelope.setOutputSoapObject(request);
                       String temp=username+"#"+password;
                       request.addProperty("unpass",temp); 
                       HttpTransportSE ht = new HttpTransportSE(URL);
                       ht.call(SOAP_ACTION, envelope); 
                       final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                       final String responseStr = response.toString();
                       runOnUiThread (new Runnable(){
                          public void run(){
                              if(responseStr.contains("Successful")){
                                  AlertDialog alert = builder1.create();
                                  alert.show();
                                  try {
                                        Class ourClass = Class.forName(getResources().getText(R.string.CorePackage) +".MainMenuActivity");
                                        Intent ourIntent = new Intent(LoginActivity.this,ourClass); //Αρχικοποίηση ενός Intent
                                        ourIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //Εξασφαλίζει ότι δεν υπάρχουν cycles στο stack των activities. 
                                        startActivity(ourIntent);   //Εκκίνηση νέου activity με βάση το ορισμένο intent
                                    } catch (ClassNotFoundException e) {
                                        Log.e(TAG,"Class not found exception");
                                    }
                              }else {
                                  myTextView = (TextView)findViewById(R.id.textView1);
                                  myTextView.setText(responseStr);
                              }

                          }
                       });
                   } catch(Exception e){
                       e.printStackTrace();
                   }
               }
           };
           networkThread.start();
    }
    });

}

}

这是我得到的错误:

03-08 17:55:15.295: E/AndroidRuntime(1957): FATAL EXCEPTION: main
03-08 17:55:15.295: E/AndroidRuntime(1957): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidpoiservice/com.androidpoiservice.LoginActivity}: java.lang.NullPointerException
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.os.Looper.loop(Looper.java:137)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at java.lang.reflect.Method.invoke(Method.java:511)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at dalvik.system.NativeStart.main(Native Method)
03-08 17:55:15.295: E/AndroidRuntime(1957): Caused by: java.lang.NullPointerException
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at com.androidpoiservice.LoginActivity.<init>(LoginActivity.java:45)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at java.lang.Class.newInstanceImpl(Native Method)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at java.lang.Class.newInstance(Class.java:1319)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
03-08 17:55:15.295: E/AndroidRuntime(1957):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
03-08 17:55:15.295: E/AndroidRuntime(1957):     ... 11 more

2 个答案:

答案 0 :(得分:3)

此行导致问题......

AlertDialog.Builder builder1 = new AlertDialog.Builder(this);

声明builder1如下......

AlertDialog.Builder builder1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);    

    builder1 = new AlertDialog.Builder(this);
}

答案 1 :(得分:1)

试试这个..

在onCreate方法中使用builder1 = new AlertDialog.Builder(this);并在全局

中添加此AlertDialog.Builder builder1;
AlertDialog.Builder builder1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);    

     builder1 = new AlertDialog.Builder(this);

}