gpt4 book ai didi

android - 当我想从另一个 listView 创建 listView 时出错

转载 作者:行者123 更新时间:2023-11-30 03:38:00 25 4
gpt4 key购买 nike

我有两个类(class)。在第一个类 ListDataActivityInterv 中,我想显示类 ListViewMultipleSelectionActivity 中的所有项目。所以我在第一个类,当我按下按钮添加时我想显示一个 listView(第二堂课),检查一个,两个项目,当我按下发送按钮时出现在第一个类。我也想存储在表格中选择的这些项目。这是我的第一个类代码:

public class ListDataActivityInterv extends Activity{

private ListView lsInterventiiView = null;
private PacientiDB pacientiDB = null;
private PacientiAdapter adapter = null;
private ArrayList<Interventii> listInterventii = null;
private Button buttonCreate2 = null;
private String text = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewinterventii);

text = getIntent().getExtras().getString("Pacienti");
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1.setText(text);

buttonCreate2 = (Button) findViewById(R.id.btnCreateInt);
buttonCreate2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(ListDataActivityInterv.this, ListViewMultipleSelectionActivity.class);
startActivity(intent);
}
});

Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
ListView lv = (ListView) findViewById(R.id.lsvListInterventii);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);
lv.setAdapter(adapter);
}

}

第二类:

public class ListViewMultipleSelectionActivity extends Activity implements
OnClickListener {
Button button;
ListView listView;
ArrayAdapter<String> adapter;
public String text = null;
private PacientiDB pacientiDB = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincheckinterv);
text = getIntent().getExtras().getString("Pacienti");
TextView text1 = (TextView) findViewById(R.id.textViewListInter);
text1.setText(text);
findViewsById();
String[] sports = getResources().getStringArray(R.array.sports_array);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, sports);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
button.setOnClickListener(this); }

private void findViewsById() {
listView = (ListView) findViewById(R.id.list);
button = (Button) findViewById(R.id.testbutton); }

public void onClick(View v) {
Interventii interventie = pacientiDB.getInterv(text);
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {

int position = checked.keyAt(i);

if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position)); }
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
interventie = new Interventii();
interventie.setDenumire(selectedItems.get(i));
pacientiDB.insertI(interventie);
}
Intent intent = new Intent(getApplicationContext(), ListDataActivityInterv.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
intent.putExtras(b);
startActivity(intent); }
}

我收到这个错误:

04-30 19:42:55.642: E/AndroidRuntime(4899): FATAL EXCEPTION: main
04-30 19:42:55.642: E/AndroidRuntime(4899): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.medicaldoi/com.example.medicaldoi.ListDataActivityInterv}: java.lang.NullPointerException
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.os.Looper.loop(Looper.java:123)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-30 19:42:55.642: E/AndroidRuntime(4899): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 19:42:55.642: E/AndroidRuntime(4899): at java.lang.reflect.Method.invoke(Method.java:521)
04-30 19:42:55.642: E/AndroidRuntime(4899): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-30 19:42:55.642: E/AndroidRuntime(4899): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-30 19:42:55.642: E/AndroidRuntime(4899): at dalvik.system.NativeStart.main(Native Method)
04-30 19:42:55.642: E/AndroidRuntime(4899): Caused by: java.lang.NullPointerException
04-30 19:42:55.642: E/AndroidRuntime(4899): at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
04-30 19:42:55.642: E/AndroidRuntime(4899): at java.util.Arrays.asList(Arrays.java:171)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
04-30 19:42:55.642: E/AndroidRuntime(4899): at com.example.medicaldoi.ListDataActivityInterv.onCreate(ListDataActivityInterv.java:45)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 19:42:55.642: E/AndroidRuntime(4899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-30 19:42:55.642: E/AndroidRuntime(4899): ... 11 more

有人可以帮助我吗?有人有其他想法吗?

最佳答案

您的字符串数组为空

        Bundle b = getIntent().getExtras();        
String[] resultArr = b.getStringArray("selectedItems");
if(resultArr==null)
finish();
ListView lv = (ListView) findViewById(R.id.lsvListInterventii);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);
lv.setAdapter(adapter);

现在检查你的应用是否直接关闭,你的字符串数组是否为空。??

您还可以检查 bundle 是否包含字符串数组......如果它包含数组则它是否为空。

b.containsKey(resultArr)

关于android - 当我想从另一个 listView 创建 listView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16308242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com