进度条未显示在登录页面上显示

时间:2014-09-24 04:38:00

标签: android android-progressbar

我在登录活动未显示之前设置的进度条。我已经尝试了很多组合,但它仍然无法正常工作。请帮帮我们,我的工作就是在线。我的老板会因此而杀了我。

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
Button login_b1;
private EditText user, pass;
private HandleJSON obj;
String passStr="";
String uname="";
String status="";
SharedPreferences SP;
String key;
public static String getUrlData= new Sql_connect().url();
 // Progress Dialog
private ProgressDialog mProgressDialog;
private static final String LOGIN_URL = getUrlData+"windex.php?itfpage=login";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        SharedPreferences SP2 = getSharedPreferences("key",MODE_PRIVATE);
        key=SP2.getString("key on click","");
        Log.v("key on create",key);
        setContentView(R.layout.activity_main);
        login_b1=(Button)findViewById(R.id.btnLogin);
        user = (EditText)findViewById(R.id.mailid);
        pass= (EditText)findViewById(R.id.pass);

        login_b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                  mProgressDialog = new ProgressDialog(MainActivity.this);
                  mProgressDialog.setMessage("Signing In..."); 
                  mProgressDialog.show();
                  uname = user.getText().toString();
                  Log.v("uname", ""+uname);
                  passStr = pass.getText().toString();
                 String finalUrl = LOGIN_URL + "&email="+uname+"&password="+passStr;
                 Log.v("uname", ""+finalUrl);
             try{
                 obj = new HandleJSON(finalUrl);
                 obj.fetchJSON();

                 while(obj.parsingComplete);

                 status=obj.status();
                 Log.v("dooStatus",status);
                 Log.v("doo",""+obj.getRegid());
                 if(!obj.getRegid().equals(null) && status.equals("success")){
                     Log.v("do in try block",""+obj.getRegid());
                     SP = getSharedPreferences("key",MODE_PRIVATE);
                     SP.edit()
                     .putString("key on click", obj.getRegid())
                     .commit();
                     Log.v("after shared",""+obj.getRegid());                
                     Intent i = new Intent(getApplicationContext(), UserPage.class);
                     startActivity(i);
                 }
                 }catch(Exception e){
                     e.printStackTrace();
                     if(status.equals("failed")){
                     Toast.makeText(getBaseContext(), "Incorrect UserId or Password ", Toast.LENGTH_LONG).show();
                     }else if(status.equals("inactive")){
                         Toast.makeText(getBaseContext(), "Please activate your account", Toast.LENGTH_LONG).show();
                     }
                }

                 mProgressDialog.dismiss(); 
            }
        });

        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });  

        TextView forgot=(TextView) findViewById(R.id.link_to_forgot);
        forgot.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent j = new Intent(getApplicationContext(), ForgotActivity.class);
                startActivity(j);   
            }
        });

}
}

2 个答案:

答案 0 :(得分:0)

您已将.show().dismiss()写入同一个区块,这就是为什么它没有显示。请改用AsyncTask来完成您的流程。这是一个教程,http://programmerguru.com/android-tutorial/android-asynctask-example/

答案 1 :(得分:0)

public class ClassName extends AsyncTask<String, String, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(String... params) {

            //write code what you want to do @background don't try to write ui code here
            return null;
        }

        @Override
        protected void onPostExecute(String file_url) {
            // after completing ur background task this function will run and u can as well write ui code right here..
            progressBar.setVisibility(View.GONE);
        }
    }