嗨,在下面的代码中,NPE
String result1 = imService.DispalyGroupDetails(imService.getUsername());
显示错误。我想要用户名。我正在使用imservice calss访问用户名。我在其他地方访问的用户名相同,但工作正常但是在下面的代码中只给出问题。
任何人都可以帮助我
** FriendList **
public class FriendList extends ListActivity
{
private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
private static final int CREATE_GROUP_ID = Menu.FIRST+1;
private static final int EXIT_APP_ID = Menu.FIRST + 2;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;
public String ownusername = new String();
private class FriendListAdapter extends BaseAdapter
{
class ViewHolder {
TextView text,text1;
ImageView icon;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friends = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friends = friends;
}
public int getCount() {
return friends.length;
}
public FriendInfo getItem(int position) {
return friends[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.friend_list_screen, null);
holder = new ViewHolder();
holder.text1=(TextView)convertView.findViewById(R.id.text1);
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friends[position].userName);
//holder.text1.steText(groupname);
holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
return convertView;
}
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
try {
String result1 = imService.DispalyGroupDetails(imService.getUsername());
System.out.println(result1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
friendAdapter = new FriendListAdapter(this);
}
** GroupList **
public class GroupList extends ListActivity
{
boolean[] checkBoxState;
boolean isChecked;
String check;
ListView users;
int position;
private IAppManager imService = null;
ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();
private FriendListAdapter friendAdapter;
public String ownusername = new String();
private class FriendListAdapter extends BaseAdapter
{
@SuppressWarnings("unused")
class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;
}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;
private FriendInfo[] friend = null;
public FriendListAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
}
public void setFriendList(FriendInfo[] friends)
{
this.friend = friends;
}
public int getCount() {
return friend.length;
}
public FriendInfo getItem(int position) {
return friend[position];
}
public long getItemId(int position) {
return 0;
}
@SuppressWarnings("unused")
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(friend[position].userName);
holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
final ArrayList<String> checkedFriends = new ArrayList<String>();
checkBoxState = new boolean[friend.length];
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;
if(isChecked){
check=friend[position].userName;
}
}
});
return convertView;
}
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.group_list_screen);
Button create=(Button)findViewById(R.id.create);
friendAdapter = new FriendListAdapter(this);
friendAdapter.getCheckedItems();
create.setOnClickListener(new OnClickListener() {
@SuppressWarnings({ "unused", "unchecked" })
@Override
public void onClick(View v) {
String groupname = getIntent().getStringExtra("nick");
try {
String result1 = imService.CreateGroup(groupname,imService.getUsername(),friendAdapter.getCheckedItems());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();
}
});
}
答案 0 :(得分:0)
您将imService定义为null:
private IAppManager imService = null;
你从未初始化它然后你试图打电话
String result1 = imService.DispalyGroupDetails(imService.getUsername());
因此nullpointer异常。您可能想要初始化imService。