1

这是我正在处理的代码。当我使用此代码运行我的应用程序时,它会停止,没有错误。

我已更改 manifest.xml,但它不起作用。

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ok=(Button)findViewById(R.id.Button01);  
        ok.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View arg0) { 
                new jsdetails().execute();


                }

        });
    }
    public class jsdetails extends AsyncTask<String,String,String> {

        Boolean validUser = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Verifying.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String doInBackground(String... args) {

            //int valoreOnPostExecute = 0;
            error=(TextView)findViewById(R.id.TextView01);
            un=(EditText)findViewById(R.id.EditText01);  
            pw=(EditText)findViewById(R.id.EditText02); 

            params.add(new BasicNameValuePair("JS_Email",un.getText().toString()));  
            params.add(new BasicNameValuePair("JS_Password",pw.getText().toString()));

            JSONObject json = jParser.makeHttpRequest(url,"POST",params); 
            Log.d("All Products: ", json.toString());
                try {  
             int success = json.getInt(TAG_SUCCESS);
                                 if (success == 1)
                 {
                     validUser = true;

                 }
                                } catch (JSONException e) {
                e.printStackTrace();
            }
                 return null;
        }
        protected void onPostExecute(String file_url) {

            pDialog.dismiss();
            if(validUser)
            {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                finish();
            }


             params.clear();
                }
            }

} 
4

5 回答 5

1

您必须对 UIThread 进行查看操作。后台线程上的异步 doInBackground 方法。您不会在后台线程中获得 editText 的文本。

于 2013-10-09T11:03:55.020 回答
0

试试这个..删除finish();

if(validUser)
{
      Intent i = new Intent(MainActivity.this, UserManage.class);
      startActivity(i);
}
于 2013-10-09T10:48:32.417 回答
0

您在 doinbackground() 方法中返回 null 而不是返回有效用户并在 onPostExecute

protected void onPostExecute(String validUser) {

        pDialog.dismiss();
        if(validUser)
        {
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
            finish();
        }


         params.clear();
            }
        }
于 2013-10-09T11:11:00.533 回答
0

像这样做

 public class jsdetails extends AsyncTask<String,String,Boolean> {

        Boolean validUser = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Verifying.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected Boolean doInBackground(String... args) {

            //int valoreOnPostExecute = 0;
            error=(TextView)findViewById(R.id.TextView01);
            un=(EditText)findViewById(R.id.EditText01);  
            pw=(EditText)findViewById(R.id.EditText02); 

            params.add(new BasicNameValuePair("JS_Email",un.getText().toString()));  
            params.add(new BasicNameValuePair("JS_Password",pw.getText().toString()));

            JSONObject json = jParser.makeHttpRequest(url,"POST",params); 
            Log.d("All Products: ", json.toString());
                try {  
             int success = json.getInt(TAG_SUCCESS);
                                 if (success == 1)
                 {
                     validUser = true;

                 }
                                } catch (JSONException e) {
                e.printStackTrace();
            }
                 return validUser;
        }
        protected void onPostExecute(Boolean validUser) {

            pDialog.dismiss();
            if(validUser)
            {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                finish();
            }


             params.clear();
                }
            }

}***
于 2013-10-09T11:32:20.647 回答
0

我做了一些改变和建议。参考以下代码。

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            error=(TextView)findViewById(R.id.TextView01);
            un=(EditText)findViewById(R.id.EditText01);      // Note
            pw=(EditText)findViewById(R.id.EditText02);
        ok=(Button)findViewById(R.id.Button01);  
        ok.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View arg0) { 
                new jsdetails(MainActivity.this).execute(un.getText().toString(),pw.getText().toString());
// Note

                }

        });
    }
    public class jsdetails extends AsyncTask<String,Integer,Boolean > {    // Note
        Context context;    // Note
        Boolean validUser = false;
        jsdetails(Context context){    // Note
           this.context=context;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Verifying.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected Boolean doInBackground(String... args) {    // Note

            //int valoreOnPostExecute = 0;


            params.add(new BasicNameValuePair("JS_Email",args[0]));      // Note
            params.add(new BasicNameValuePair("JS_Password",args[1]));

            JSONObject json = jParser.makeHttpRequest(url,"POST",params); 
            Log.d("All Products: ", json.toString());
                try {  
             int success = json.getInt(TAG_SUCCESS);
                                 if (success == 1)
                 {
                     validUser = true;

                 }
                                } catch (JSONException e) {
                e.printStackTrace();
            }
                 return validUser ;
        }
        protected void onPostExecute(String result) {

            pDialog.dismiss();
            if(result)    // Note
            {
                Intent i = new Intent(context, UserManage.class);    // Note
                startActivity(i);
                finish();
            }


             params.clear();
                }
            }

}
于 2013-10-09T11:38:34.127 回答