有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文本视图工作不正常

因此,我正在构建这个安卓应用程序,我正在处理一条错误消息,当用户输入错误的用户名或密码时,将显示该消息。此时会偶尔显示错误消息。我收到一个异常呼叫

安卓。看法viewroot$CalledFromErrorThreadException

当我只进行研究时,作者基本上说,当您使用AsyncTask时,在doinBackground方法中设置文本不是一个好主意。由于在onPostExecute中设置测试将随时显示登录是否成功的方法,因此我提出的部分解决方案是这样的

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

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ListAdapter;
import 安卓.widget.SimpleAdapter;
import 安卓.widget.TextView;

public class Login extends Activity {
    static EditText alog;
    static EditText apass;
    JSONParser jsnParser = new JSONParser();
    public static String url_login="...";
    private static final String TAG_SUCCESS = "operation";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent it =getIntent();
        setContentView(R.layout.activity_login);
         alog=(EditText)findViewById(R.id.loginuser);
         apass=(EditText)findViewById(R.id.loginpass);

          Button btnLogin = (Button)findViewById(R.id.loggin);
          Button btnRegister =(Button)findViewById(R.id.register2);

          btnRegister.setOnClickListener(new View.OnClickListener() {


                public void onClick(View view) {

                    Intent i=new Intent(Login.this, Registration.class);
                    startActivity(i);
                }
            });

          btnLogin.setOnClickListener(new View.OnClickListener() {


            public void onClick(View view) {

                new LoggerIn().execute();
            }
        });


    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    class LoggerIn  extends AsyncTask<String, String, String> {

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

            String logger=alog.getText().toString();
            String passer=apass.getText().toString();



        List <NameValuePair> prm=new ArrayList<NameValuePair>();
        prm.add(new BasicNameValuePair("username", logger));
        prm.add(new BasicNameValuePair("password", passer));

        JSONObject jsn= jsnParser.makeHttpRequest(url_login, "POST", prm);


        Log.d("Create Response", jsn.toString());
        try {
            boolean success=jsn.getBoolean(TAG_SUCCESS);

            if (success == true) {
                // successfully created product
                Intent i = new Intent(getApplicationContext(), Home.class);

                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
                //onPostExecute
                //TextView txt=(TextView)findViewById(R.id.login_error);
                //txt.setText("The username or password you entered was wrong");
                displayError();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
        }

        protected void displayError() {
            // dismiss the dialog after getting all products

            // updating UI from Background Thread
            TextView txt=(TextView)findViewById(R.id.login_error);
            txt.setText("The username or password you entered was wrong");

        }
    }



}

但是,当我登录失败时,我必须在消息出现之前重复失败2到3次。有没有合适的方法


共 (2) 个答案

  1. # 1 楼答案

    只能从UI线程更新UI。从可以访问UI线程的方法执行Asynctask中的所有UI调用,即onPreExecute()onProgressUpdate()onPostExecute()

  2. # 2 楼答案

    问题似乎是您试图在AsyncTask内的UI线程上执行某些操作。如果要更改UI线程(即显示Toast、设置TextView等),请使用onPostExecute()方法或使用runOnUiThreaddoInBackground内进行更改

    请参阅上面的问题,该问题进一步解释了您遇到的错误以及如何更正:How to fix android.os.NetworkOnMainThreadException?

    请参见此处如何从AsyncTask内调用UI线程上的方法: Android : Calling the methods on UI thread from AsyncTask doInBackground method

    希望这有帮助