当前位置: 首页 > 知识库问答 >
问题:

我正试图从activity发送数据到碎片,但我得到这个错误,任何可以帮助请?

艾成益
2023-03-14

这是我在这里输入图像描述的错误

public class ProfileFragment extends Fragment {
TextView profileNames,ProfileRegNumber;
String regnumber;
String Student_URL ="http://192.168.43.244/ULKandroid/student.php";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_profile,container,false);
    //HomeActivity homeActivity = new HomeActivity();
    profileNames = (TextView) v.findViewById(R.id.txtProfileNames);
    ProfileRegNumber = (TextView) v.findViewById(R.id.txtProfileRegNo);

    String n = this.getArguments().getString("names");
    String r = this.getArguments().getString("reg");

    profileNames.setText(n);
    ProfileRegNumber.setText(r);

    return v;
}

}

我得到的错误是这样的

11-08 11:54:12.604 415 4-4154/Cards.inkuge.com.ulkStudent e/AndroidRuntime:致命异常:主进程:Cards.inkuge.com.ulkStudent,PID:4154 java.lang.nullPointerException位于Cards.inkuge.com.ulkStudent.Fragments.ProfileFragment.onCreateView(ProfileFragment.java:46)位于AndroidX.Fragment.App.Fragment.PerformCreateView(Fragment.java:2600)位于在Androidx.fragment.app.fragmentManagerimpl.execpendingActions(FragmentManagerimpl.java:1727)在Androidx.fragment.app.fragmentManagerimpl.java:1727)在Android.os.handler.handleCallback(handler.java:733)在Android.os.handler.dispatchMessage(handler.java:95)在Android.os.looper.loop(looper.java:136)在Android.app.activitythread.main(

  1. 列表项

这是一个设置参数的activity吗

else {
                                        Intent intent = new Intent(LoginActivity.this,HomeActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putString("names",jsonObject.getString("student_names"));
                                        bundle.putString("reg",jsonObject.getString("regnumber_student"));
                                        ProfileFragment fragment = new ProfileFragment();
                                        bundle.putString("names",jsonObject.getString("student_names"));
                                        bundle.putString("reg",jsonObject.getString("regnumber_student"));
                                        fragment.setArguments(bundle);
                                        intent.putExtras(bundle);
                                        startActivity(intent);
                                        clear();
                                        finish();
                                    }

共有1个答案

梁烨
2023-03-14

您应该创建类似于以下内容的片段:

 public static ProfileFragment NewInstance(String names, String reg) {
    ProfileFragment fragment = new ProfileFragment();

    Bundle bundle = new Bundle();
    bundle.putString("names", names);
    bundle.putString("reg", reg);
    fragment.setArguments(bundle);
    return fragment;
}

还要确保您有这样的构造函数

 public ProfileFragment() {
    // Required empty public constructor
}

在此片段中定义全局变量,如下所示:

private String _names;
private String _reg;

重写onCreate方法,如下所示:

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    if(getArguments() != null) {
        _names= getArguments().getString("names");
        _reg= getArguments().getString("reg");
    }
}

然后在OnCrateView方法中设置如下所示的文本:

profileNames.setText(_names);
ProfileRegNumber.setText(_reg);

在activity创造出这样的碎片。

private void replaceFragmentInternal(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter_fragment, R.anim.exit_fragment);

            fragmentManager.popBackStack();
            fragmentTransaction
                    .addToBackStack(null)
                    .replace(R.id.frameLayoutHolder, fragment)
                    .commit();
    }


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        replaceFragmentInternal(ProfileFragment.NewInstance(names,reg));
}
 类似资料: