我正在尝试从待处理活动中获取选中的listview
个项目到 allowed_to activity
。这是我的代码:
pending.java
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[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
allowed_to.class);
b = new Bundle();
b.putStringArray("nibba", StrArr);
intent.putExtras(b);
startActivity(intent);
arrayAdapter.remove(str);
}
});
allowed_to.java
public class allowed_to extends pending {
public static String[] resultArr;
public static ListView lvl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allowed_to);
Bundle bundle = getIntent().getExtras();
resultArr = bundle.getStringArray("nibba");
lvl = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, resultArr);
lvl.setAdapter(adapter);
Toast.makeText(getApplicationContext(), "Allowed successfully !!",
Toast.LENGTH_SHORT).show();
}
}
但是,每当我打开应用程序时,都会出现此错误,并且我的应用程序崩溃。
logcat
11-14 19:55:21.782 28136-28136/com.nevermiss.subhamdutta.nevermiss E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nevermiss.subhamdutta.nevermiss, PID: 28136
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nevermiss.subhamdutta.nevermiss/com.nevermiss.subhamdutta.nevermiss.allowed_to}: java.lang.NullPointerException: **Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference**
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
更新:我还尝试检查是否放置if语句,如下所示:
package com.nevermiss.subhamdutta.nevermiss;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import static android.R.attr.data;
public class allowed_to extends pending {
public static String[] resultArr;
public static ListView lvl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allowed_to);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
resultArr = bundle.getStringArray("nibba");
lvl = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, resultArr);
lvl.setAdapter(adapter);
Toast.makeText(getApplicationContext(), "Allowed successfully !!",
Toast.LENGTH_SHORT).show();
}
}
}
幸运的是,该应用程序已打开,但是listview
完全为空。如何查看这些listview
个项目?
答案 0 :(得分:0)
String[] StrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
StrArr[i] = selectedItems.get(i);
}
我认为,您的错误就在此刻。所选项目出了点问题。记录该项目的所有值,然后将其放入捆绑包。我认为您选择的项目为空。
答案 1 :(得分:0)
您的类allowed_to
扩展了pending
(一个真实的Activity),并且可能扩展了它的上下文。所以当你打电话
Bundle bundle = getIntent().getExtras();
您实际上是在尝试获取pending
活动捆绑包。
所以我最好的猜测是像allowed_to
那样使AppCompatActivity
扩展pending
。