APK文件已成功构建,没有错误,但应用程序无法正常工作

时间:2017-03-10 14:29:16

标签: android android-studio

我开发了一个连接到数据库的Android应用程序。登录功能已成功完成。当我添加注册功能时,应用程序停止工作。 APK文件已成功构建,主要活动也成功启动,但是当我点击任何按钮时,应用程序停止。这是主要的活动代码:`public class MainActivity扩展AppCompatActivity {

SQLiteDatabase db;
String url;
EditText emailText, passText;

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

    final TextView registerText = (TextView) findViewById(R.id.registertext);
    final Button loginbtn = (Button) findViewById(R.id.login);

    db = openOrCreateDatabase("User", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Login(ID VARCHAR, NAME VARCHAR, pass VARCHAR);");

    registerText.setOnClickListener(
            new TextView.OnClickListener() {
                public void onClick(View view){
                    Intent intent = new Intent(MainActivity.this, Register.class);
                    startActivity(intent);
                }
            }
        );

    loginbtn.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View view) {

                    //check connection
                    ConnectivityManager cm = (ConnectivityManager) getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
                    NetworkInfo netInfo = cm.getNetworkInfo(0);
                    url = "http://skillsexchangecyprus.com/SEC/SkillsLogin.php";

                    //Check fields
                    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);

                    final TextView alert = (TextView)findViewById(R.id.alert);


                    if (emailText.getText().toString().matches(" ") || passText.getText().toString().matches(" ")) {
                        Snackbar snackbar = Snackbar.make(relativeLayout, "Fill In Empty Fields", Snackbar.LENGTH_LONG);
                        snackbar.show();
                    } else {
                        new BackgroundTasks(alert).execute(url);
                    }
                }});}


            class BackgroundTasks extends AsyncTask <String, Void, String> {
                TextView alert;

                public BackgroundTasks(TextView textview) {
                    this.alert = textview;
                }

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    alert.setVisibility(View.VISIBLE);
                    alert.setText("Loading...");
                }
                String emtxt= emailText.getText().toString();
                String passtxt= passText.getText().toString();

                @Override
                protected String doInBackground(String... strings) {
                    String result = "";

                    try {
                        URL url = new URL(strings[0]);
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);

                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setDoOutput(true);

                        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

                        final EditText emailText = (EditText) findViewById(R.id.email);
                        final EditText passText = (EditText) findViewById(R.id.password);



                        params.add(new BasicNameValuePair("email", emtxt));
                        params.add(new BasicNameValuePair("password", passtxt));

                        OutputStream os = urlConnection.getOutputStream();
                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                        writer.write(getQuery(params));
                        writer.flush();
                        writer.close();
                        os.close();

                        InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                        StringBuilder builder = new StringBuilder();

                        String inputString;
                        while ((inputString = bufferedReader.readLine()) != null) {
                            builder.append(inputString);
                        }
                        result = String.valueOf(builder.toString());
                        urlConnection.disconnect();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return result;
                }

                @NonNull
                private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
                    StringBuilder out = new StringBuilder();
                    boolean first = true;

                    for (NameValuePair pair : params) {
                        if (first)
                            first = false;
                        else
                            out.append("&");

                        out.append(URLEncoder.encode(pair.getName(), "UTF-8"));
                        out.append("=");
                        out.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
                    }
                    return out.toString();
                }

                @Override
                protected void onPostExecute(String temp) {

                    if (temp.trim().matches("Empty")) {
                        alert.setText("Invalid username or password");
                    } else {
                        alert.setVisibility(View.GONE);
                        String[] Split = temp.split("_");

                        db.execSQL("INSERT INTO Login values(' " + Split[1] + " ' , ' " + Split[0] + " ', ' " + Split[3] + " ')");

                        Intent intent = new Intent(MainActivity.this, FindSkill.class);
                        startActivity(intent);
                    }
                }
            }

}`

这里是注册码:

public class Register extends AppCompatActivity {

String url;
EditText name,email,pass;
Button registerBtn;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);


    registerBtn.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View view) {

                    ConnectivityManager cm = (ConnectivityManager) getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
                    NetworkInfo netInfo = cm.getNetworkInfo(0);
                    url="http://skillsexchangecyprus.com/SEC/SkillsRegister.php";

                    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.regLayout);
                    name = (EditText) findViewById(R.id.namereg);
                    email = (EditText) findViewById(R.id.emailreg);
                    pass = (EditText) findViewById(R.id.passwordreg);
                    final TextView alert = (TextView)findViewById(R.id.alert);

                    if (name.getText().toString().matches("")||email.getText().toString().matches("")||pass.getText().toString().matches("")) {
                        Snackbar snackbar = Snackbar
                                .make(relativeLayout, "Fill in Empty Fields", Snackbar.LENGTH_LONG);
                        snackbar.show();
                    } else {
                        new BackgroundTasks(alert).execute(url);
                    }
                    }});}

                class BackgroundTasks extends AsyncTask<String, Void, String> {
                    TextView alert;

                    public BackgroundTasks(TextView textview) {
                        this.alert = textview;
                    }

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        alert.setVisibility(View.VISIBLE);
                        alert.setText("Saving....");
                    }

                    String nametxt = name.getText().toString();
                    String emtxt = email.getText().toString();
                    String passtxt = pass.getText().toString();

                    @Override
                    protected String doInBackground(String... strings) {
                        String task = "";
                        try {
                            URL url = new URL(strings[0]);
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);

                            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                            urlConnection.setDoOutput(true);

                            ArrayList<NameValuePair> params = new ArrayList<>();
                            params.add(new BasicNameValuePair("nameReg", nametxt));
                            params.add(new BasicNameValuePair("emailReg", emtxt));
                            params.add(new BasicNameValuePair("passwordReg", passtxt));

                            OutputStream os = urlConnection.getOutputStream();
                            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                            writer.write(getQuery(params));
                            writer.flush();
                            writer.close();
                            os.close();
                            InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
                            StringBuilder builder = new StringBuilder();

                            String inputString;
                            while ((inputString = bufferedReader.readLine()) != null) {
                                builder.append(inputString);
                            }

                            task = String.valueOf(builder.toString());

                            urlConnection.disconnect();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return task;
                    }

                    @NonNull
                    private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
                        StringBuilder result = new StringBuilder();
                        boolean first = true;

                        for (NameValuePair pair : params) {
                            if (first)
                                first = false;
                            else
                                result.append("&");

                            result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
                            result.append("=");
                            result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
                        }

                        return result.toString();
                    }

                    @Override
                    protected void onPostExecute(String temp) {

                        if (temp.trim().matches("Success")) {
                            alert.setText("Successfully saved");
                        } else {
                            String msg = "NOT FOUND";
                            alert.setVisibility(View.VISIBLE);
                            alert.setText("Try again");
                        }
                    }
                }

}

现在Logcat充满了错误和警告!!!

1 个答案:

答案 0 :(得分:0)

你在做什么是错的。

String emtxt= emailText.getText().toString();
                String passtxt= passText.getText().toString();

                @Override
                protected String doInBackground(String... strings) {
                    String result = "";

                    try {
                        URL url = new URL(strings[0]);
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);

                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setDoOutput(true);

                        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

                        final EditText emailText = (EditText) findViewById(R.id.email);
                        final EditText passText = (EditText) findViewById(R.id.password);

您正在尝试在后台方法中创建视图对象,并且您尝试在此之前从它们获取值。相应地重新格式化代码并检查。