应用程序随机崩溃

时间:2017-11-27 21:33:12

标签: android

所以我构建了一个连接到我的服务器的Android应用程序,并通过Wi-Fi向其他用户发送短信。服务器工作正常但我无法弄清楚为什么每次我尝试使用该应用程序时它会在我登录时崩溃。

logcat输出:

canner.findPatternInBuffer(Scanner.java:1022)
11-27 17:13:43.089 32571-32571/? W/System.err:     at java.util.Scanner.findWithinHorizon(Scanner.java:1694)
11-27 17:13:43.089 32571-32571/? W/System.err:     at java.util.Scanner.nextLine(Scanner.java:1555)
11-27 17:13:43.089 32571-32571/? W/System.err:     at com.thatadhacker.texting.MainActivity.onCreate(MainActivity.java:43)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.Activity.performCreate(Activity.java:6942)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.ActivityThread.-wrap14(ActivityThread.java)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.os.Looper.loop(Looper.java:154)
11-27 17:13:43.089 32571-32571/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6682)
11-27 17:13:43.089 32571-32571/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
public static Socket s;
public static PrintWriter out;
public static Scanner in;
public static Button login;
public static Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    login = (Button) findViewById(R.id.button);
    send = (Button) findViewById(R.id.button2);
    login.setOnClickListener(this);
    send.setOnClickListener(this);
    new Thread(new Runnable(){
        public void run() {
            try {
                s = new Socket("finlaybot.soapfudge.com", 5567);
                out = new PrintWriter(s.getOutputStream(), true);
                in = new Scanner(s.getInputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
            EditText text = (EditText) findViewById(R.id.editText4);
            while (true) {
                text.append(in.nextLine());
            }
        }
    }).start();
}
@Override
public void onClick(final View view){
    new Thread(new Runnable(){
        public void run() {
            switch (view.getId()) {
                case R.id.button:
                    EditText user = (EditText) findViewById(R.id.editText);
                    EditText pass = (EditText) findViewById(R.id.editText2);
                    String username = user.getText().toString();
                    String password = pass.getText().toString();
                    out.println(username);
                    out.println(password);
                    user.setText("");
                    pass.setText("");
                    break;
                case R.id.button2:
                    EditText to = (EditText) findViewById(R.id.textView);
                    EditText message = (EditText) findViewById(R.id.editText4);
                    out.println(to.getText().toString());
                    out.println(message.getText().toString());
                    to.setText("");
                    message.setText("");
                    break;
            }
        }
    }).start();
}

我知道服务器工作正常,因为桌面客户端工作正常。 *编辑我得到了日志

1 个答案:

答案 0 :(得分:0)

您正尝试从非UI /主线程的线程访问/修改UI控件。在日志中,您应该看到类似

的内容
  

只有创建视图层次结构的原始线程才能触及其视图。

您应该在onCreate中创建一个处理程序并发布到该处理程序或使用AsyncTaskLoader。通过查看代码,这是我能想到的唯一问题。尝试附加日志。 处理程序的例子

public class MainActivity extends AppCompatActivity {
Button mButton;
Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mHandler = new Handler();
    mButton = (Button) findViewById(R.id.button);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mButton.setText("rushan");
                }
            });
            mButton.setText("new Text");
        }
    }).start();
}

}