I want to make an chat application in android. I was making it like simple java application using socket but it is not working. Pls suggest what I should do.
This is my code. On the click of button, app is closing forcely..
package pack.chat;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.io.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class chatAct extends Activity implements android.view.View.OnClickListener {
/** Called when the activity is first created. */
PrintWriter out;
BufferedReader in ;
Socket socketClient;
EditText edit1;
String editTextString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
socketClient= new Socket( "hddlntd6014578 ", 8180);
out = new PrintWriter(socketClient.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
}
catch (UnknownHostException e){
System.out.println("Host cannt be reached");
}
catch (IOException i){
System.out.println("IO cannt be found");
}
Button searchButton = (Button)findViewById(R.id.Button01);
searchButton.setOnClickListener(this);
// InputStreamReader input= new InputStreamReader();
// BufferedReader stringForServer= new BufferedReader(new InputStreamReader(R.id.EditText01));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit1= (EditText)findViewById(R.id.EditText01);
System.out.println(edit1.getText().toString());
String forServer = edit1.getText().toString();
out.println(forServer);
}
}
You can extract that I am a new bee. Please suggest something. If you have a code and if u can then pls share that with me.
Log CAT::
05-11 15:54:39.614: WARN/dalvikvm(370): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): FATAL EXCEPTION: main
05-11 15:54:39.624: ERROR/AndroidRuntime(370): java.lang.NullPointerException
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at pack.chat.chatAct.onClick(chatAct.java:61)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at android.view.View.performClick(View.java:2485)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at android.view.View$PerformClick.run(View.java:9080)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at android.os.Handler.handleCallback(Handler.java:587)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at android.os.Looper.loop(Looper.java:123)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at android.app.ActivityThread.main(ActivityThread.java:3647)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 15:54:39.624: ERROR/Androi开发者_Python百科dRuntime(370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): at dalvik.system.NativeStart.main(Native Method)
05-11 15:54:39.634: WARN/ActivityManager(61): Force finishing activity pack.chat/.chatAct
05-11 15:54:40.164: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{40672790 pack.chat/.chatAct}
You don't have variable out
defined. So out.print*
will NPE.
You may use System.out.print*
on that line or initialize out
before using it.
I also missed this when looking over the code :) If you use an IDE like Eclipse, try increasing warning levels for potential NPEs and the like.
I have already make a application for chatting using C2DM (push mail) this is the mew way to send data among android devices. I think you should you C2DM cloud to device messaging technology of android for making this application. I hope this is help.
精彩评论