运行此代码时发生异步致命错误。
我正在制作一个应用程序来控制远程距离的计算机,如团队查看器。
package pk.edu.cust.fyp.nobeen.sameer.umair.pccontroller;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
Button connectBtn;
EditText ipAddressEditTxt;
String ipAddress;
int port=4444;
boolean connectionResult = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectBtn = (Button) findViewById(R.id.connect);
ipAddressEditTxt = (EditText) findViewById(R.id.ipEditText);
ipAddress = ipAddressEditTxt.getText().toString();
connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClientConnection clientConnection = new ClientConnection();
clientConnection.execute(ipAddress);
if(connectionResult == true) {
Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_SHORT).show();
Intent intent;
intent = new Intent(MainActivity.this, Login.class);
startActivity(intent);
}
else
Toast.makeText(getApplicationContext(),"Connection Failed",Toast.LENGTH_SHORT).show();
}
});
}
class ClientConnection extends AsyncTask <String,String,String>
{
Socket socket;
DataInputStream dataInputStream;
DataOutputStream dataOutputStream;
Context context;
String TAG ="Client Connection";
@Override
protected String doInBackground(String... params) {
Toast.makeText(getApplicationContext(),"doInBackground run", Toast.LENGTH_SHORT).show();
try {
socket = new Socket(params.toString(), port);
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
Toast.makeText(getApplicationContext(),"Connection is succesfully "+dataInputStream.readUTF(),Toast.LENGTH_SHORT).show();
if(socket.isConnected())
{
connectionResult=true;
}
}catch (Exception ex)
{
Toast.makeText(getApplicationContext(),"Exception occur: "+ex,Toast.LENGTH_SHORT).show();
//Log.e(TAG,ex.toString());
connectionResult = false;
}
return null;
}
protected void onPreExecute()
{
super.onPreExecute();
}
protected void onPostExecute(String s){
Toast.makeText(getApplicationContext(),"OnPost"+connectionResult,Toast.LENGTH_SHORT).show();
//super.onPostExecute(s);
}
}
}
答案 0 :(得分:-1)
问题:您正在后台显示Toast而不运行UI线程。从doInBackground()方法中删除Toast消息
如果您想要显示toasts,请在onPreExecute()和onPostExecute()方法中显示它们,因为这些方法在UI线程中调用,但是在单独的线程中调用doInBackground()。