2016-11-19 39 views
2

こんにちは私はどのようにDBに登録情報を入力するかについて取り組んでいます。私は、ログインページの「今すぐ登録」ボタンをクリックすると、アプリがシャットダウンします。私は、この問題のために何をすべきか?ボタンをクリックした直後にアプリケーションが終了しました

Login.javaコード

public class Login extends Activity { 
    EditText ET_NAME,ET_PASS; 
    String login_name,login_pass; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login_main); 
     ET_NAME = (EditText)findViewById(R.id.user_name); 
     ET_PASS = (EditText)findViewById(R.id.user_pass); 
    } 
    public void userReg(View view) 
    { 
     startActivity(new Intent(this,Register.class)); 
    } 
    public void userLogin(View view) 
    { 
     login_name = ET_NAME.getText().toString(); 
     login_pass = ET_PASS.getText().toString(); 
     String method = "login"; 
     BackgroundTask backgroundTask = new BackgroundTask(this); 
     backgroundTask.execute(method,login_name,login_pass); 
    } 
} 

Register.java

public class Register extends Activity { 
    EditText ET_NAME,ET_USER_NAME,ET_USER_PASS; 
    String name,user_name,user_pass; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.register_layout); 
     ET_NAME = (EditText)findViewById(R.id.name); 
     ET_USER_NAME= (EditText)findViewById(R.id.new_user_name); 
     ET_USER_PASS = (EditText)findViewById(R.id.new_user_pass); 
    } 
    public void userReg(View view) 
    { 
     name = ET_NAME.getText().toString(); 
     user_name = ET_USER_NAME.getText().toString(); 
     user_pass =ET_USER_PASS.getText().toString(); 
     String method = "register"; 
     BackgroundTask backgroundTask = new BackgroundTask(this); 
     backgroundTask.execute(method,name,user_name,user_pass); 
     finish(); 

    } 

とログ。

No apk changes detected since last installation, skipping installation of C:\Android\1113GMDemo1\app\build\outputs\apk\app-debug.apk 
$ adb shell am force-stop com.example.jina.a1105gmdemo 
$ adb shell am start -n "com.example.jina.a1105gmdemo/com.example.jina.a1105gmdemo.Login" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D 
Connecting to com.example.jina.a1105gmdemo 
I/System.out: Sending WAIT chunk 
W/ActivityThread: Application com.example.jina.a1105gmdemo is waiting for the debugger on port 8100... 
I/dalvikvm: Debugger is active 
I/System.out: Debugger has connected 
I/System.out: waiting for debugger to settle... 
Connected to the target VM, address: 'localhost:8600', transport: 'socket' 
I/System.out: waiting for debugger to settle... 
I/System.out: waiting for debugger to settle... 
I/System.out: waiting for debugger to settle... 
I/System.out: waiting for debugger to settle... 
I/System.out: waiting for debugger to settle... 
I/System.out: waiting for debugger to settle... 
I/System.out: waiting for debugger to settle... 
I/System.out: debugger has settled (1427) 
I/MultiDex: VM with version 1.6.0 does not have multidex support 
I/MultiDex: install 
I/MultiDex: MultiDexExtractor.load(/data/app/com.example.jina.a1105gmdemo-19.apk, false) 
I/MultiDex: loading existing secondary dex files 
I/MultiDex: load found 1 secondary dex files 
I/MultiDex: install done 
I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 
D/dalvikvm: threadid=1: still suspended after undo (sc=1 dc=1) 
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket' 

私は他の点を選んでデバッグするとき、私はこのようなログました。

I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: () 
       OpenGL ES Shader Compiler Version: E031.24.00.08 
       Build Date: 03/21/14 Fri 
       Local Branch: AU200+patches_03212014 
       Remote Branch: 
       Local Patches: 
       Reconstruct Branch: 
D/OpenGLRenderer: Enabling debug mode 0 
D/OpenGLRenderer: GL error from OpenGLRenderer: 0x502 
E/OpenGLRenderer: GL_INVALID_OPERATION 

+ これは、バックグラウンドコードである

public class BackgroundTask extends AsyncTask<String,Void,String> { 
    AlertDialog alertDialog; 
    Context ctx; 

    BackgroundTask(Context ctx) { 
     this.ctx = ctx; 
    } 

    @Override 
    protected void onPreExecute() { 
    // alertDialog = new AlertDialog.Builder(ctx).create(); 
    // alertDialog.setTitle("Login Information"); 
    // alertDialog.setMessage("Login success"); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String reg_url = "http://35.160.135.119/webapp/register.php"; 
     String login_url = "http://35.160.135.119/webapp/login.php"; 
     String method = params[0]; 
     if (method.equals("register")) { 
      String name = params[1]; 
      String user_name = params[2]; 
      String user_pass = params[3]; 
      try { 
       URL url = new URL(reg_url); 
       final String COOKIES_HEADER = "Set-Cookie"; // stackoverflow 

       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 

       java.net.CookieManager msCookieManager = new java.net.CookieManager(); 
       Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields(); 
       List<String> cookiesHeader = headerFields.get(COOKIES_HEADER); 

       if (cookiesHeader != null) { 
        for (String cookie : cookiesHeader) { 
         msCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); 
        } 
       } 
       if (msCookieManager.getCookieStore().getCookies().size() > 0) { 
        // While joining the Cookies, use ',' or ';' as needed. Most of the servers are using ';' 
        httpURLConnection.setRequestProperty("Cookie", 
          TextUtils.join(";", msCookieManager.getCookieStore().getCookies())); 
       } 

       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       //httpURLConnection.setDoInput(true); 
       OutputStream OS = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
       String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + 
         URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
         URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 


       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       OS.close(); 
       InputStream IS = httpURLConnection.getInputStream(); 
       IS.close(); 
       //httpURLConnection.connect(); 
       httpURLConnection.disconnect(); 
       return "Registration Success..."; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } else if (method.equals("login")) { 
      String login_name = params[1]; 
      String login_pass = params[2]; 
      try { 
       URL url = new URL(login_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
       String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" + 
         URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
       String response = ""; 
       String line = ""; 
       while ((line = bufferedReader.readLine()) != null) { 
        response += line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return response; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (result.equals("Registration Success...")) { 
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
     } else { 
      alertDialog.setMessage(result); 
      alertDialog.show(); 
     } 
    } 
} 

答えて

0

(仕上げを削除)この方法でUSERREG(ビュービュー).Actuallyあなたは()仕上げを呼び出すことによって、活動を終えています。

public void userReg(View view) 
{ 
    name = ET_NAME.getText().toString(); 
    user_name = ET_USER_NAME.getText().toString(); 
    user_pass =ET_USER_PASS.getText().toString(); 
    String method = "register"; 
    BackgroundTask backgroundTask = new BackgroundTask(this); 
    backgroundTask.execute(method,name,user_name,user_pass); 
    //finish(); remove it 

} 
+0

ああ、私はそれをしましたが、動作しませんでした。そして、Registerアクティビティでは、レジスタ情報を編集した後に終了しなければなりません。 –

+0

BackgroundTaskコードを投稿してください... – sasikumar

+0

この行のコメントを解除してください。alertDialog = new AlertDialog.Builder(ctx).create(); onPreExecute() – sasikumar

0

これが役立つかどうかはわかりません。しかし、onPostExecuteメソッドでNullPointerExceptionが発生する可能性があります。これは、resultdoInBackgroundによって返され、nullになる可能性があるためです。 equals式を反転して、もう一度お試しください。

+0

ありがとう!メインアクティビティをログインアクティビティから移動する際に機能します。今私はなぜ私はmysqlにレジスタのデータを挿入することができないのかを調べる必要があります。 –

+0

それと幸運:) – hqzxzwb

関連する問題