lundi 2 février 2015

NPE friendAdapter.getCheckedItems(); in android

Hi in the below code I am getting the NPE at the line `friendAdapter.getCheckedItems();'.Actually I want only checked values From checkbox.


And Then I want to pass the selected values I want to return as array and passed as parameter to the method createGroup()


Can any one help me. Why i am getting this error


java



public class GroupList extends ListActivity
{
boolean[] checkBoxState;
boolean isChecked;
String[] check;
String[] mStringArray;
ListView users;
int position;
int i;
private IAppManager imService = null;

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;
}
public ArrayList<FriendInfo> getCheckedItems(){
ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();
for (int i = 0; i < checkBoxState.length; i++) {
if(checkBoxState[i]){
result.add(getItem(i));
System.out.print(result);
}
}
return result;

}
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;


}
});

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.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(),check);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();
}
});
friendAdapter = new FriendListAdapter(this);
}




Aucun commentaire:

Enregistrer un commentaire