我的困境是我从一个对话片段来到我的weighInFragment有两条信息:一个日期和一个int。我希望能够使用这两条信息并构建一个需要添加到现有Arrayadapter的newEntry。 arraylist显示在weighInFragment上,我使用dialogfragment捕获新条目的信息。但是,当我尝试将新条目添加到适配器时,我得到一个nullpointer异常,我认为这是由于适配器为空。所以我想知道如何才能获得此适配器并能够添加新条目?
这是y onActivityResult,这是我在对话框中点击“保存”后降落的地方:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case DATEPICKER_FRAGMENT:
if (resultCode == Activity.RESULT_OK) {
Bundle bundle=data.getExtras();
Date date = (Date) bundle.getSerializable("date");
int weight = (Integer) bundle.getSerializable("weight");
Log.d("NewEntry", "Date is: " + date + "Weight: " +weight );
WeighInAdapter adapter = (WeighInAdapter)getListAdapter();
WeighInEntry newEntry = new WeighInEntry();
newEntry.setDate(date);
newEntry.setWeight(weight);
adapter.add(newEntry);
adapter.notifyDataSetChanged();
} else if (resultCode == Activity.RESULT_CANCELED){
}
break;
}
}
这是我在对话适配器中获取数据的地方:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
// LayoutInflater inflater = getActivity().getLayoutInflater();
final View v = getActivity().getLayoutInflater()
.inflate(R.layout.weigh_in_dialog, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(v)
// Add action buttons
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatePicker datePicker = (DatePicker)v.findViewById(R.id.datePicker1);
EditText weightAmount = (EditText) v.findViewById(R.id.weight);
mDate = getDateFromDatePicket(datePicker);
mWeight = Integer.parseInt(weightAmount.getText().toString());
Intent i = new Intent();
Bundle extras=new Bundle();
extras.putSerializable("date", mDate); //putString("date",Month);
extras.putInt("weight",mWeight);
i.putExtras(extras);
getTargetFragment().onActivityResult(getTargetRequestCode(),Activity.RESULT_OK,i);
dismiss() ;
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
NewEntryDialogFragment.this.getDialog().cancel();
Log.d("CANCEL WAS PRESSED:", "!!!!!!!!!!!!");
}
});
return builder.create();
}
这是logcat错误:
11-17 17:42:51.050: D/NewEntry(2593): Date is: Tue Nov 17 17:42:51 EST 2015Weight: 200
11-17 17:42:51.060: D/AndroidRuntime(2593): Shutting down VM
11-17 17:42:51.060: W/dalvikvm(2593): threadid=1: thread exiting with uncaught exception (group=0xb1ae9b90)
11-17 17:42:51.070: E/AndroidRuntime(2593): FATAL EXCEPTION: main
11-17 17:42:51.070: E/AndroidRuntime(2593): Process: edu.bu.juanl.finalproject, PID: 2593
11-17 17:42:51.070: E/AndroidRuntime(2593): java.lang.NullPointerException
11-17 17:42:51.070: E/AndroidRuntime(2593): at edu.bu.juanl.finalproject.WeighInFragment.onActivityResult(WeighInFragment.java:141)
11-17 17:42:51.070: E/AndroidRuntime(2593): at edu.bu.juanl.finalproject.NewEntryDialogFragment$1.onClick(NewEntryDialogFragment.java:84)
11-17 17:42:51.070: E/AndroidRuntime(2593): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-17 17:42:51.070: E/AndroidRuntime(2593): at android.os.Handler.dispatchMessage(Handler.java:102)
11-17 17:42:51.070: E/AndroidRuntime(2593): at android.os.Looper.loop(Looper.java:137)
11-17 17:42:51.070: E/AndroidRuntime(2593): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-17 17:42:51.070: E/AndroidRuntime(2593): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 17:42:51.070: E/AndroidRuntime(2593): at java.lang.reflect.Method.invoke(Method.java:515)
11-17 17:42:51.070: E/AndroidRuntime(2593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-17 17:42:51.070: E/AndroidRuntime(2593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-17 17:42:51.070: E/AndroidRuntime(2593): at dalvik.system.NativeStart.main(Native Method)
11-17 17:42:53.440: I/Process(2593): Sending signal. PID: 2593 SIG: 9
我尝试了很多东西,我无法弄明白!
答案 0 :(得分:0)
DialogPickerFragment输入来自用户的数据,这需要反映在包含列表视图的不同对话框片段中。
数据正在传递给父活动,并且它正在尝试访问属于listview片段的元素。这就是问题所在。该活动将无法访问片段列表适配器。
你应该做的是在activity和listview片段之间实现一个接口并传入数据。查看android开发者指南:http://developer.android.com/training/basics/fragments/communicating.html