A
Android Central Question
I have been trying to send checked listview items to another *listview* of another activity with the help of bundle. And I can successfully transfer the listview items to the other listview.
But the problem arises when I try to assign a button/tab to that *listview*. It crashes as soon as I press the button/tab, showing a null pointer exception. Please help me fix this.
Code snippet for the first listview is given below-
button.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
String str = null;
for (int i = 0; i < checked.size(); i++) {
int position;
position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(String.valueOf(arrayAdapter.getItem(position)));
str = listView.getItemAtPosition(i).toString();
if(ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(pending.this,new String[]{Manifest.permission.SEND_SMS},MY_PERMISSIONS_REQUEST_SEND_SMS );
}
else
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(str,null,"#def",null,null);
Toast.makeText(getApplicationContext(), "Affirmation sent !!",
Toast.LENGTH_SHORT).show();
}
}
String[] StrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
StrArr = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
allowed_to.class);
Bundle b = new Bundle();
b.putStringArray("nibba", StrArr);
intent.putExtras(b);
startActivity(intent);
arrayAdapter.remove(str);
}
});
Code snippet for the listview of the other activity is given below-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//instance = this;
setContentView(R.layout.allowed_to);
Bundle b = getIntent().getExtras();
resultArr = b.getStringArray("nibba");
ListView lv1 = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, resultArr);
lv1.setAdapter(adapter);
Toast.makeText(getApplicationContext(), "Allowed successfully !!",
Toast.LENGTH_SHORT).show();
}
The error is shown as-
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
But the problem arises when I try to assign a button/tab to that *listview*. It crashes as soon as I press the button/tab, showing a null pointer exception. Please help me fix this.
Code snippet for the first listview is given below-
button.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
String str = null;
for (int i = 0; i < checked.size(); i++) {
int position;
position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(String.valueOf(arrayAdapter.getItem(position)));
str = listView.getItemAtPosition(i).toString();
if(ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(pending.this,new String[]{Manifest.permission.SEND_SMS},MY_PERMISSIONS_REQUEST_SEND_SMS );
}
else
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(str,null,"#def",null,null);
Toast.makeText(getApplicationContext(), "Affirmation sent !!",
Toast.LENGTH_SHORT).show();
}
}
String[] StrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
StrArr = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
allowed_to.class);
Bundle b = new Bundle();
b.putStringArray("nibba", StrArr);
intent.putExtras(b);
startActivity(intent);
arrayAdapter.remove(str);
}
});
Code snippet for the listview of the other activity is given below-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//instance = this;
setContentView(R.layout.allowed_to);
Bundle b = getIntent().getExtras();
resultArr = b.getStringArray("nibba");
ListView lv1 = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, resultArr);
lv1.setAdapter(adapter);
Toast.makeText(getApplicationContext(), "Allowed successfully !!",
Toast.LENGTH_SHORT).show();
}
The error is shown as-
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference