2017-03-10 12 views
1

データベースに接続されているAndroid Appを開発しています。ログインが正常に終了しました。登録機能を追加すると、アプリは動作を停止しました。 APKファイルが正常に作成され、メインアクティビティも正常に起動しますが、いずれかのボタンをクリックするとアプリケーションが停止します。ここでの主な活動コードがある: `パブリッククラスMainActivityはAppCompatActivity {APKファイルは正常に作成されましたが、エラーは発生しませんでしたが、アプリケーションが動作していません

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がいっぱいですエラーと警告!!!

+0

アプリがクラッシュした場合にもエラーを投稿してください – RobCo

+0

今、Logcatにはメモがいっぱいです! – user233531

答えて

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); 

ビュー・オブジェクトをバックグラウンド・メソッドで作成しようとしていますが、その前にそれらから値を取得しようとしています。それに応じてコードを再フォーマットして確認してください。

関連する問題