Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android newInstance();在片段中接收空值_Android_Android Fragments - Fatal编程技术网

Android newInstance();在片段中接收空值

Android newInstance();在片段中接收空值,android,android-fragments,Android,Android Fragments,你好 在将变量数据从活动发送到片段时,我遇到了一个问题 这是我的密码 Activity.java if (savedInstanceState == null) { Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no); getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragmen

你好

在将变量数据从活动发送到片段时,我遇到了一个问题

这是我的密码

Activity.java

    if (savedInstanceState == null) {
        Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
        getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();
        navigationView.setCheckedItem(R.id.voter_home);
    }
然后在

Fragment.java

    if (savedInstanceState == null) {
        Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
        getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();
        navigationView.setCheckedItem(R.id.voter_home);
    }
公共类Fragment\u Voter\u Home扩展了Fragment{

private static final String ARG_STUDNO = "stud_no";

private String student_no;

public static Fragment_Voter_Home newInstance(String student_no) {

    Fragment_Voter_Home fragment = new Fragment_Voter_Home();
    Bundle args = new Bundle();
    args.putString(ARG_STUDNO, student_no);
    fragment.setArguments(args);
    return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.fragment_voter_home, container, false);
    TextView textView = v.findViewById(R.id.voter_fragment_textview);

    if (getArguments() != null) {
        student_no = getArguments().getString(ARG_STUDNO);
    }

    textView.setText("Welcome, "+student_no);

    return v;

}
}
这是我的片段.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<TextView
    android:id="@+id/voter_fragment_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_vertical|center_horizontal|center"
    android:text="Home"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>

然后在运行之后,这就是我得到的

我已经尝试过这个线程中的解决方案,但没有一个有效


我是android开发新手。谢谢。

因为您的代码不正确,请将代码更改为

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                fragment).commit();
而不是

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();

由于代码不正确,请将代码更改为

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                fragment).commit();
而不是

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();

问题是您将片段实例化如下:

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
然后,您没有将其传递到
FragmentTransaction
,而是传递了一个
新片段\u Voter\u Home()


这使得包含已传递参数的实例化片段未被使用,然后传递到片段堆栈中的片段包含未初始化的参数,导致
null

错误在于,您是这样实例化片段的:

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
然后,您没有将其传递到
FragmentTransaction
,而是传递了一个
新片段\u Voter\u Home()

这使得包含传递参数的实例化片段未被使用,然后传递到片段堆栈中的片段包含未初始化的参数,这导致
null