我正在构建一个由webscokets和XAMPP驱动的聊天应用程序(暂时)。但是,在尝试发送消息时,应用程序只会出错并记下错误:
02-22 22:47:26.571 1439-1622/com.example.feastapp E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-140
Process: com.example.feastapp, PID: 1439
java.lang.NullPointerException
at com.example.specialSharing.SpecialSharing$4$1.run(SpecialSharing.java:259)
在imService.sendMessage("user1","user2", "1", "0", "0") == null
行。
我将服务绑定到类(在类的顶部):
private Manager imService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((MessagingService.IMBinder) service).getService();
Toast.makeText(getApplicationContext(), "Service Connected", Toast.LENGTH_LONG).show();
}
public void onServiceDisconnected(ComponentName className) {
imService = null;
Toast.makeText(SpecialSharing.this, R.string.local_service_stopped,
Toast.LENGTH_SHORT).show();
}
};
我面临的问题是我在整个应用程序中多次使用此代码绑定服务,然后使用上述方法发送消息。
为什么现在该动作会产生NullPointer?为什么这里的服务不起作用?
public class SpecialSharing extends Activity {
private Switch mapAppearSwitch;
private TextView selectLocationText;
private EditText enteredMessage;
private MapFragment mapFragment;
private GoogleMap googleMap;
private LatLngBounds latLngBounds;
private Button sendButton;
private Button changeShareButton;
private Integer shareType = 0;
private String sharedSpecialId;
private String selectedLocationId = null;
private InfoOfGroup group = new InfoOfGroup();
private InfoOfFriend friend = new InfoOfFriend();
String getSpecialLocationURL = null;
String specialLocationActionURL = "http://" + Global.getIpAddress()
+ ":3000/getSpecialLocations/";
String JSON = ".json";
JSONArray resultObject;
ArrayList<String> locationIdList = new ArrayList<String>();
ArrayList<String> locationAddressList = new ArrayList<String>();
ArrayList<String> locationCityList = new ArrayList<String>();
ArrayList<String> locationStateList = new ArrayList<String>();
ArrayList<String> locationZipList = new ArrayList<String>();
ArrayList<Double> locationLatitudeList = new ArrayList<Double>();
ArrayList<Double> locationLongitudeList = new ArrayList<Double>();
String[] locationId = new String[locationIdList.size()];
String[] locationAddress = new String[locationAddressList.size()];
String[] locationCity = new String[locationCityList.size()];
String[] locationState = new String[locationStateList.size()];
String[] locationZip = new String[locationZipList.size()];
Double[] locationLatitude = new Double[locationLatitudeList.size()];
Double[] locationLongitude = new Double[locationLongitudeList.size()];
private Manager imService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((MessagingService.IMBinder) service).getService();
Toast.makeText(getApplicationContext(), "Service Connected", Toast.LENGTH_LONG).show();
}
public void onServiceDisconnected(ComponentName className) {
imService = null;
Toast.makeText(SpecialSharing.this, R.string.local_service_stopped,
Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sharing_action);
// Get activity objects:
// mapFragment = (MapFragment) getFragmentManager()
// .findFragmentById(R.id.restaurantLocationMap);
// googleMap = mapFragment.getMap();
mapAppearSwitch = (Switch) findViewById(R.id.switchLocationSharing);
selectLocationText = (TextView) findViewById(R.id.selectLocationText);
sendButton = (Button) findViewById(R.id.sendSharing);
changeShareButton = (Button) findViewById(R.id.changeShareButton);
enteredMessage = (EditText) findViewById(R.id.enterMessage);
// Get switch
mapAppearSwitch = (Switch) findViewById(R.id.switchLocationSharing);
// Initialize settings:
mapAppearSwitch.setChecked(false);
selectLocationText.setVisibility(View.INVISIBLE);
// mapFragment.getView().setVisibility(View.INVISIBLE);
// Getting either the group or friend to share with:
Bundle extras = this.getIntent().getExtras();
group.groupName = extras.getString(InfoOfGroup.GROUPNAME);
group.groupId = extras.getString(InfoOfGroup.GROUPID);
friend.userName = extras.getString(InfoOfFriend.USERNAME);
//******** NEED FRIENDS USER ID ********
// Getting the selected id:
SharedPreferences settings = getSharedPreferences("SHARED_SPECIAL_ID", 0);
sharedSpecialId = settings.getString("specialId", "0");
//Toast.makeText(getApplicationContext(), "Special shared id is " +specialId, Toast.LENGTH_LONG).show();
// Check if a group of friend sharing with, then set the button
if (group.groupName != null) {
changeShareButton.setText("Group: " + group.groupName);
shareType = 0;
} else if (friend.userName != null) {
changeShareButton.setText(friend.userName);
shareType = 1;
}
// Toggling location message or not
mapAppearSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
mapFragment.getView().setVisibility(View.VISIBLE);
selectLocationText.setVisibility(View.VISIBLE);
} else {
mapFragment.getView().setVisibility(View.INVISIBLE);
selectLocationText.setVisibility(View.INVISIBLE);
}
}
});
// Change share button
changeShareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Have dialog to confirm, and open sharing back up
// To select a different sharing
}
});
// Send message
sendButton.setOnClickListener(new OnClickListener() {
Handler handler = new Handler();
public void onClick(View arg0) {
if (selectedLocationId == null) {
selectedLocationId = "0";
}
Thread thread = new Thread() {
public void run() {
try {
if (imService.sendMessage("user1",
"user2", "1", "0", "0") == null) {
handler.post(new Runnable() {
public void run() {
Toast.makeText(
getApplicationContext(),
R.string.message_cannot_be_sent,
Toast.LENGTH_LONG).show();
}
});
}
} catch (UnsupportedEncodingException e) {
Toast.makeText(getApplicationContext(),
R.string.message_cannot_be_sent,
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
};
thread.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sharing_action, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}