このコードを実行している間、非同期の致命的なエラーが発生します。AsyncTask致命的なエラー#1
チームビューアのように遠隔地にあるコンピュータを制御するアプリケーションを作っています。
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);
}
}
}
同じエラーの質問:https://stackoverflow.com/questions/23558855/fatal-exception-asynctask-1-android –
実際のlogcatを含めるように質問してください –