我有两节课。在第一个类 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
有人能帮我吗?有人有其他想法吗?