用于将数据从一个片段传递到另一个片段的空指针异常

时间:2016-10-11 12:30:03

标签: android android-fragments

我试图在界面的帮助下将警报对话框 EditText 中的数据发送到另一个片段。

  • 我在fragements和主要活动中遇到了Null Pointer Exception的问题。

AlertFragment代码:

public class AlertDialogFragment extends DialogFragment {
DialogClick dClick;
protected AlertDialog adAlert;
public AlertDialogFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    dClick = (DialogClick)activity;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder adDetails;
    adDetails = new AlertDialog.Builder(getContext());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    adDetails.setTitle("Enter the Details");
    adDetails.setCancelable(true);
    adDetails.setView(inflater.inflate(R.layout.fragment_dialog, null,false));
    adDetails.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Dialog view = (Dialog) dialogInterface;
            TextInputEditText Fname = (TextInputEditText) view.findViewById(R.id.edtFname);
            TextInputEditText Lname = (TextInputEditText) view.findViewById(R.id.edtLname);
            String sFname = Fname.getText().toString();
            String sLname = Lname.getText().toString();
            dClick.onDialogPositiveClick(sFname, sLname);
        }
    });
    adDetails.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        dismiss();
        }
    });
    adAlert = adDetails.create();
    return adDetails.create();
}}

ListFragment

public class ListViewFragment extends ListFragment {
View view;
TextView txt;
TextView txt2;
public ListViewFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.activity_memo_main, container, false);
    txt = (TextView) view.findViewById(R.id.tvFName);
    txt2 = (TextView) view.findViewById(R.id.tvLName);
    return view;
}
public void sendData(String Fname, String Lname){
    txt.setText(Fname);
    txt2.setText(Lname);
}}

MainActivity

public class MemoMainActivity extends AppCompatActivity implements DialogClick {
private FragmentManager fragManger ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memo_main);
    init();
    setupDefaults();
    setupEvents();
}

public void init() {
    fragManger = getSupportFragmentManager();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.menuAdd:
            AlertDialogFragment altDialFrag = new AlertDialogFragment();
            altDialFrag.show(fragManger,null);
            break;
    }
    return super.onOptionsItemSelected(item);
}
public void setupDefaults(){

}
public void setupEvents(){

}
@Override
public void onDialogPositiveClick(String sFname, String sLname){
    ListViewFragment list = new ListViewFragment();
    list.sendData(sFname,sLname);
}}

记录猫:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.Fragemnt.Memo, PID: 27307
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at com.Fragemnt.Memo.ListViewFragment.sendData(ListViewFragment.java:27)
                  at com.Fragemnt.Memo.MemoMainActivity.onDialogPositiveClick(MemoMainActivity.java:52)
                  at com.Fragemnt.Memo.AlertDialogFragment$1.onClick(AlertDialogFragment.java:45)
                  at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5221)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

3 个答案:

答案 0 :(得分:1)

使用Bundle传递片段之间的值

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

通过这种方式,您可以发送值

然后在你的片段中,用:

检索数据(例如在onCreate()方法中)
 Bundle bundle = this.getArguments();
if (bundle != null)
    int myInt = bundle.getInt(key, defaultValue);

希望这有用:)

答案 1 :(得分:1)

在Fragment中使用 getActivity.findViewById

您可以使用Bundle

发送数据
YourFragmentName fragment = new YourFragmentName();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

答案 2 :(得分:0)

您的ListView片段未附加到活动,此片段中的所有视图此时都不会膨胀

ListViewFragment list = new ListViewFragment();
list.sendData(sFname,sLname);

因此,首先需要将片段设置为容器,然后通过Bundle参数发送字符串数据。如果需要,也请阅读此内容 Best practice for instantiating a new Android Fragment