2016-02-17 115 views
-2

我想在Android中創建一個登錄程序。我爲這個登錄程序編寫了代碼。在此程序中,我將用戶名和密碼發送到服務器進行身份驗證。但是,當我打開應用程序並輸入用戶名和密碼並單擊登錄按鈕時,我的應用程序不幸停止。但用戶名和密碼將發送到服務器。比我的應用程序將停止,當我點擊登錄按鈕。這是我的代碼。不幸的是,Android應用程序已停止

public class LogIn extends Activity implements View.OnClickListener { 

    private Socket socket; 
    private DataOutputStream out; 
    private DataInputStream in; 

    private String username,password,s; 

    EditText eT_username,eT_password; 
    Button b_login,b_signup,b_connect; 
    TextView t; 

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

     eT_username = (EditText)findViewById(R.id.eT_username); 
     eT_password = (EditText)findViewById(R.id.eT_password); 

     b_login = (Button)findViewById(R.id.b_login); 
     b_signup = (Button)findViewById(R.id.b_signup); 
     b_connect = (Button)findViewById(R.id.b_connect); 

     t = (TextView)findViewById(R.id.tV); 

     b_login.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     String toast_msg; 
     try { 
      out = new DataOutputStream(socket.getOutputStream()); 
      in = new DataInputStream(socket.getInputStream()); 

      out.writeUTF("1"); 

      username = eT_username.getText().toString(); 
      password = eT_password.getText().toString(); 

      out.writeUTF(username); 
      //  out.flush(); 
      out.writeUTF(password); 
      //  out.flush(); 

      t.setText("hi"); 

      s = in.readUTF(); 


      if (s.equals("ok")) { 
       toast_msg = "Login is successfull"; 
       t.setText(toast_msg); 
      } else { 
       toast_msg = "username or password is incorrect"; 
       t.setText(toast_msg); 
      } 

     } catch (IOException e) { 
      t.setText(e.toString()); 
     } 
    } 

    public void connect(View v) 
    { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try 
       { 
        socket = new Socket("192.168.0.101",5000); 

       } 
       catch(Exception e) 
       { 
        TextView t = (TextView)findViewById(R.id.textView); 
        t.setText(e.toString()); 
        // Toast.makeText(LogIn.this,"Make sure that you are connected to Internet",Toast.LENGTH_LONG).show(); 
       } 
      } 
     }).start(); 
    } 

    public void openSignUp(View v) 
    { 
     if (v.getId()== R.id.b_signup) 
     { 
      Intent i = new Intent(LogIn.this,SignUp.class); 
      startActivity(i); 
     } 
     new SignUp(socket); 
    } 
} 

錯誤:

02-17 23:34:34.340 2156-2156/? E/Zygote: v2 
02-17 23:34:34.340 2156-2156/? I/libpersona: KNOX_SDCARD checking this for 10118 
02-17 23:34:34.340 2156-2156/? I/libpersona: KNOX_SDCARD not a persona 
02-17 23:34:34.340 2156-2156/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G531F_5.1.1_0020 
02-17 23:34:34.340 2156-2156/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 
02-17 23:34:34.340 2156-2156/? I/art: Late-enabling -Xcheck:jni 
02-17 23:34:34.350 2156-2156/? D/TimaKeyStoreProvider: in addTimaSignatureService 
02-17 23:34:34.350 2156-2156/? D/TimaKeyStoreProvider: TimaSignature is unavailable 
02-17 23:34:34.350 2156-2156/? D/ActivityThread: Added TimaKesytore provider 
02-17 23:34:34.350 2156-2156/? I/SAMP: ActivityThread() - SAMP_ENABLE : true 
02-17 23:34:34.450 2156-2156/? D/ContextImpl: ContextImpl running for user UserHandle{0} 0 
02-17 23:34:34.470 2156-2156/kiran.onlinejavacompiler D/ContextImpl: ContextImpl running for user UserHandle{0} 0 
02-17 23:34:34.470 2156-2156/kiran.onlinejavacompiler W/ResourcesManager: getTopLevelResources: null for user 0 
02-17 23:34:34.490 2156-2156/kiran.onlinejavacompiler D/ContextImpl: ContextImpl running for user UserHandle{0} 0 
02-17 23:34:34.500 2156-2156/kiran.onlinejavacompiler D/ContextImpl: ContextImpl running for user UserHandle{0} 0 
02-17 23:34:34.500 2156-2156/kiran.onlinejavacompiler W/ResourcesManager: getTopLevelResources: null for user 0 
02-17 23:34:34.510 2156-2156/kiran.onlinejavacompiler W/ResourcesManager: getTopLevelResources: null for user 0 
02-17 23:34:34.510 2156-2156/kiran.onlinejavacompiler D/DisplayManager: DisplayManager() 
02-17 23:34:34.560 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* installDecor mIsFloating : false 
02-17 23:34:34.560 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* installDecor flags : -2139029248 
02-17 23:34:34.600 2156-2199/kiran.onlinejavacompiler D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
02-17 23:34:34.630 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
02-17 23:34:34.630 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* isFloatingMenuEnabled return false 
02-17 23:34:34.670 2156-2199/kiran.onlinejavacompiler I/OpenGLRenderer: Initialized EGL, version 1.4 
02-17 23:34:34.670 2156-2199/kiran.onlinejavacompiler D/GC: <tid=2199> OES20 ===> GC Version : GC version rls_5011p6_GC5.5.13 
02-17 23:34:34.680 2156-2199/kiran.onlinejavacompiler D/OpenGLRenderer: Enabling debug mode 0 
02-17 23:34:34.840 2156-2156/kiran.onlinejavacompiler I/Timeline: Timeline: Activity_idle id: [email protected] time:6983437 
02-17 23:34:36.730 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
02-17 23:34:38.940 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
02-17 23:34:41.900 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
02-17 23:34:44.820 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
02-17 23:34:44.900 2156-2156/kiran.onlinejavacompiler D/AndroidRuntime: Shutting down VM 
02-17 23:34:44.900 2156-2156/kiran.onlinejavacompiler E/AndroidRuntime: FATAL EXCEPTION: main Process: kiran.onlinejavacompiler, PID: 2156 android.os.NetworkOnMainThreadException 
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249) 
at libcore.io.IoBridge.recvfrom(IoBridge.java:553) 
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485) 
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37) 
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237) 
at libcore.io.Streams.readFully(Streams.java:81) 
at java.io.DataInputStream.readShort(DataInputStream.java:152) 
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:165) 
at java.io.DataInputStream.readUTF(DataInputStream.java:169) 
at kiran.onlinejavacompiler.LogIn.onClick(LogIn.java:65) 
at android.view.View.performClick(View.java:5076) 
at android.view.View$PerformClick.run(View.java:20279) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5910) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
02-17 23:34:46.350 2156-2156/kiran.onlinejavacompiler I/Process: Sending signal. PID: 2156 SIG: 9 
+0

你是什麼例外? – jbarat

+0

請發佈您的logcat輸出,以便我們可以看到與崩潰報告的錯誤。 – NoChinDeluxe

+0

包含'Log.e(「登錄問題」,「錯誤捕獲」,e);'在你的catch塊中,並提供給我們堆棧跟蹤,請 – xAF

回答

0

你永遠不調用connect()方法,所以socketonClick()方法,其中您嘗試使用它來獲得輸出流null

但是,然後再次修復這將是不夠的。你會在單獨的線程中實例化socket,然後嘗試在主線程(「UI線程」)中實際使用套接字,這又會導致應用程序崩潰。

也許只是將所有的網絡操作放入一個AsyncTask

0

你可能正試圖在主線程連接,讓一個線程並連接此,

在你的代碼

另一個問題是連接方法,附上這樣的expcetion(對不起,我的英語)

public void connect(View v) 
{ 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try 
      { 
       socket = new Socket("192.168.0.101",5000); 

      } 
      catch(Exception e) 
      { 

       Runneable runException = new Runnable() { 
       @Override 
       public void run() { 
       TextView t = (TextView)findViewById(R.id.textView); 
       t.setText(e.toString()); 
       // Toast.makeText(LogIn.this,"Make sure that you are connected to Internet",Toast.LENGTH_LONG).show(); 
      }; 
     runOnUiThread(runException); 
      } 
     } 
    }).start(); 
} 
相關問題