空指针异常

q5661889 2015-06-13 11:34:44
login类
package com.example.HeartinHeart;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;



import tools.FriendEntity;
import tools.User;

import manager.ClientConService;
import manager.Transit;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends Activity{

EditText usernameedit,userpasswordedit;
TextView username,userpassword;
Button login;
Socket socket;
private Context context;
public static ArrayList<String> friend;
ObjectOutputStream out;
ObjectInputStream in;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
context=getApplicationContext();
setContentView(R.layout.loginmain);
usernameedit=(EditText)findViewById(R.id.loginnameedit);
userpasswordedit=(EditText)findViewById(R.id.loginpasswordedit);
username=(TextView)findViewById(R.id.logingname);
userpassword=(TextView)findViewById(R.id.loginpassword);
login=(Button)findViewById(R.id.loginmain);

login.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread() {
public void run() {
try {
socket = new Socket("10.254.14.5",20000);
if(socket!=null)System.out.println("555");
out= new ObjectOutputStream(socket.getOutputStream());

Information msg=new Information();
msg.setusername(usernameedit.getText().toString().trim());
msg.setUserpassword(userpasswordedit.getText().toString().trim());
msg.setrequest("login");
out.writeObject(msg);
out.flush();
in= new ObjectInputStream(new BufferedInputStream(
socket.getInputStream()));

System.out.println("//");

msg=((Information)in.readObject());

String type=msg.getType();
System.out.println(type);

if(type.equals("success"))

{
String account=msg.getAccount();
friend=msg.getFriend();

System.out.println("999");
User.account=account;
User.friend=friend;

for(int i=0;i<User.getFriend().size();i++)
{
System.out.println(User.friend.get(i));
}
ClientConService client=new ClientConService(Login.this, socket);
Intent startIntent = new Intent(Login.this, ClientConService.class);
startService(startIntent);

Transit.AddItem( account, client);
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Login.this, MainActivity.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Login.this.finish();
System.out.println("888");

}

}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}}.start();}});

}
}









ClientConService类
package manager;

import java.io.IOException;
import java.io.ObjectInputStream;

import java.net.Socket;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.example.HeartinHeart.Information;

public class ClientConService extends Service{
Socket socket;
Context context;
ObjectInputStream in;
Intent intent;
String TAG="hehe";
Intent intent1;
public ClientConService(Context context,Socket socket){
this.socket=socket;
this.context=context;
}
public ClientConService(){

}
public Socket getSocket()
{return socket;}

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() executed");

}


@Override
//当调用者使用startService()方法启动Service时,该方法被调用
public void onStart(Intent intent, int startId)
{
Log.i(TAG, "Service onStart--->");
super.onStart(intent, startId);
new Thread(new Runnable() {

public void run(){
try {
Information msgreceive=new Information();
in= new ObjectInputStream(socket.getInputStream());
while(true){
msgreceive=(Information)in.readObject();
String type=msgreceive.getType();
System.out.println(type);
if(type.equals("message")){
String m=msgreceive.getInformation();
System.out.println(m);
intent1=new Intent("friend_message");
intent1.putExtra("message", m);
context.sendBroadcast(intent1);
}
else
{
intent1=new Intent("add_friend_state");

intent1.putExtra("state", type);
context.sendBroadcast(intent1);
}
}}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}).start();

}




@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() executed");
}

@Override
public IBinder onBind(Intent intent) {
return null;
}




}

报错显示ClientConService类52行空指针
...全文
147 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hare_ 2015-06-13
  • 打赏
  • 举报
回复
看日志,错误在哪一行?

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧