Android聊天应用程序提供java:lang Null Poniter Exception

时间:2014-07-09 06:10:24

标签: android

以下是我的完整代码我正在尝试发送图片消息

它提供空指针异常

它还打印了下一行仍然给出NPE的所有值

此代码在str = imService.sendImageMes​​sage上提供例外(" sendImageMes​​sage"," fUserName","纬度","经度", "文件&#34);

package at.vcity.androidim;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import at.vcity.androidim.interfaces.IAppManager;
import at.vcity.androidim.services.IMService;
import at.vcity.androidim.types.FriendInfo;

public class ViewSendSnap extends Activity implements LocationListener{
private IAppManager imService;
//For Image
private Button buttonSendImage, buttonCancel;
private String selectedImagePath;
protected static ImageView image1;
//For GPS Location
protected LocationManager locationManager;
protected LocationListener locationListener;
String provider;
protected String latitude="NA",longitude="NA";

private String fUserName;
private String fPort;
private String fIp;
int response = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_send_snap); //messaging_screen);

new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((IMService.IMBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
imService = null;
Toast.makeText(ViewSendSnap.this, R.string.local_service_stopped,
Toast.LENGTH_SHORT).show();
}
};

selectedImagePath=getIntent().getExtras().getString("image");
fUserName=getIntent().getExtras().getString(FriendInfo.USERNAME);
fPort=getIntent().getExtras().getString(FriendInfo.PORT);
fIp="114.143.190.162";//getIntent().getExtras().getString(FriendInfo.IP);
/*System.out.println("selectedImagePath="+selectedImagePath);
System.out.println("fUserName="+fUserName);
System.out.println("fPort="+fPort);
System.out.println("fIp="+fIp);*/

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

File imgFile = new File(selectedImagePath);
image1 = (ImageView) findViewById(R.id.imageView1);
image1.setVisibility(View.VISIBLE);
image1.setImageURI(Uri.fromFile(imgFile));

buttonCancel =(Button) findViewById(R.id.ButtonCancel);
buttonCancel.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), Messaging.class);
i.putExtra(FriendInfo.USERNAME, fUserName);
i.putExtra(FriendInfo.PORT, fPort);
i.putExtra(FriendInfo.IP, fIp);
startActivity(i);
//Intent myIntent = new Intent( getApplicationContext(), Messaging.class);
//startActivityForResult(myIntent, 0);
}
});

buttonSendImage =(Button) findViewById(R.id.ButtonSendImage);
buttonSendImage.setOnClickListener(new OnClickListener(){
String file ="xyz";
Handler handler1 = new Handler();
public void onClick(View arg0) {
/*System.out.println("onClick selectedImagePath="+selectedImagePath);
System.out.println("onClick latitude="+latitude);
System.out.println("onClick longitude="+longitude);*/
Bitmap image = BitmapFactory.decodeFile(selectedImagePath);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(image, 400, 400, true);
try {
file = getByteArrayFromImage(scaledBitmap);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Thread thread1 = new Thread(){
@Override
public void run() {
try {
String str ="fail";
System.out.println("fUserName..........="+fUserName);
System.out.println("latitude..........="+latitude);
System.out.println("longitude..........="+longitude);

str = imService.sendImageMessage("sendImageMessage", "fUserName", "latitude","longitude", "file");

System.out.println("imService.sendMessage responce..........="+str);


handler1.post(new Runnable(){

public void run() {

//Toast.makeText(getApplicationContext(),R.string.message_cannot_be_sent, Toast.LENGTH_LONG).show();

System.out.println("imService.sendMessage responce..........FAIL");
//showDialog(MESSAGE_CANNOT_BE_SENT);
}

});

} catch (UnsupportedEncodingException e) {
Toast.makeText(getApplicationContext(),R.string.message_cannot_be_sent, Toast.LENGTH_LONG).show();

e.printStackTrace();
}
}
};
thread1.start();



}
});
}
private String getByteArrayFromImage(Bitmap bitmap) throws FileNotFoundException, IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, bos);
byte[] data = bos.toByteArray();
String file = Base64.encodeToString(data,Base64.DEFAULT);
return file;
}


@Override
public void onLocationChanged(Location location) {
latitude= Double.toString(location.getLatitude());
longitude=Double.toString(location.getLongitude());
}

@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}

}

1 个答案:

答案 0 :(得分:0)

NullPointerException

正在抛出imService

您已将其声明为

private IAppManager imService;

但是你需要将类的对象初始化为

imService = new IAppManager();

Object Initialization in java

希望这有帮助。