Log cat显示Null Exception错误

时间:2014-01-08 16:33:50

标签: android json phpmyadmin android-logcat

我无法理解以下日志cat错误。在使用php验证登录详细信息时 MySQL日志猫显示错误。无法验证登录密码是否匹配

01-08 22:02:44.341: D/dalvikvm(336): GC_FOR_MALLOC freed 438K, 47% free 3581K/6663K, external 1849K/2137K, paused 86ms
01-08 22:02:44.853: I/System.out(336): Exception : null
01-08 22:02:45.481: W/InputManagerService(70): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40777108

以下是Android PHP Connection的java文件。

public class AndroidPHPConnectionDemo extends Activity {
    Button b;
    EditText et,pass;
    TextView tv;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b = (Button)findViewById(R.id.Button01);  
        et = (EditText)findViewById(R.id.username);
        pass= (EditText)findViewById(R.id.password);
        tv = (TextView)findViewById(R.id.tv);

        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(AndroidPHPConnectionDemo.this, "", 
                        "Validating user...", true);
                 new Thread(new Runnable() {
                        public void run() {
                            login();                          
                        }
                      }).start();               
            }
        });
    }

    void login(){
        try{            

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://www.edu.jholhai.com/user_login.php"); // make sure the url is correct.

            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);

            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
            nameValuePairs.add(new BasicNameValuePair("user_name",et.getText().toString().trim())); 

            // $Edittext_value = $_POST['Edittext_value'];
            nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            //Execute HTTP Post Request
            response=httpclient.execute(httppost);

            // edited by James from coderzheaven.. from here....
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            System.out.println("Response : " +response); 
            runOnUiThread(new Runnable() {
                public void run() {
                    tv.setText("Response from PHP : " +response);
                    dialog.dismiss();
                }
            });

            if(response.equalsIgnoreCase("User Found")){
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(AndroidPHPConnectionDemo.this,"Login Success", Toast.LENGTH_SHORT).show();
                    }
                });

                startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
            }else{
                showAlert();                
            }

        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }
    public void showAlert(){
        AndroidPHPConnectionDemo.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(AndroidPHPConnectionDemo.this);
                builder.setTitle("Login Error.");
                builder.setMessage("User not Found.")  
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                           }
                       });                     
                AlertDialog alert = builder.create();
                alert.show();               
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

System.out.println("Response : " +response); 
        runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("Response from PHP : " +response);
                dialog.dismiss(); // <=== NullPointerException
            }
        });

您正在尝试关闭在showAlert()方法中创建的对话框,但该方法在执行

后被调用
dialog.dismiss();