Je suis en train d'écrire un simple code client serveur pour échanger des messages, mais mon application se termine lorsque je tente en cours d'exécution client.Following est mon code pour le client:Pourquoi mon application se termine-t-elle?
public class Client extends AppCompatActivity {
public void SMessage() throws IOException {
EditText ip = (EditText) findViewById(R.id.ip);
String ipadd, edttxt, output;
PrintStream ps = null;
Intent i;
Scanner sc = null;
ipadd = ip.getText().toString();
Socket s = null;
s = new Socket(ipadd,1234);
i = new Intent(getApplicationContext(), new_activity.class);
startActivity(i);
ScrollView Rl = (ScrollView) findViewById(R.id.scrollView);
sc = new Scanner(s.getInputStream());
EditText et = (EditText) findViewById(R.id.message_txt);
edttxt = et.getText().toString();
ps = new PrintStream(s.getOutputStream());
ps.println(edttxt);
TextView tv1 = new TextView(this);
tv1.setText(edttxt);
tv1.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tv1.setBackground(getDrawable(R.drawable.ic_action_msg));
}
tv1.setGravity(View.FOCUS_LEFT);
Rl.addView(tv1);
output = sc.nextLine();
if (output.equals("END")) {
s.close();
return;
}
TextView tv2 = new TextView(this);
tv2.setText(output);
tv2.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tv2.setBackground(getDrawable(R.drawable.ic_action_msg));
}
tv2.setGravity(View.FOCUS_RIGHT);
Rl.addView(tv2);
}
}
Connexion lorsque l'application se termine:
E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.devanshi.testapp, PID: 2325
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.devanshi.testapp.Client.SMessage(Client.java:27)
at com.example.devanshi.testapp.MainActivity$2.run(MainActivity.java:60)
Activité principale quand j'appelle client est la suivante:
public void client(View view) {
final Client c = new Client();
Thread t=new Thread(){
public void run(){
try {
c.SMessage();
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
}
Je voudrais savoir où est-ce que je me trompe et comment puis-je résoudre. Merci d'avance pour votre réponse. :)
vous ne devriez pas appeler l'activité du client en créant une nouvelle instance (new Client()). L'activité doit être démarrée en utilisant startActivity (intention) seulement –
Je n'ai pas compris la raison? –
En fait, j'ai une activité nommée nouvelle activité où le serveur et le client vont répondre, donc pour implémenter la classe client, ne suis-je pas censé appeler la classe client? Comment l'intention va-t-elle m'aider avec ça? –