片段 RadioButton 中的空指针崩溃

null pointer crash in fragment RadioButton

代码:
PlusOneFragment.java:

public class PlusOneFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    static Context cntxt ;
    static String []Stops;
    static int count_stops;
    Button next_page;

    public PlusOneFragment(Context c, int count_stopsArg, String [] StopsArg) {
        // Required empty public constructor
        cntxt = c;
        count_stops = count_stopsArg;
        Stops = StopsArg;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PlusOneFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PlusOneFragment newInstance(String param1, String param2) {
        PlusOneFragment fragment = new PlusOneFragment(cntxt, count_stops, Stops);
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        //TextView tv= (TextView) view.findViewById(R.id.seat_serial_prefix);
        //tv.setText("yourText");
        next_page = (Button) container.findViewById(R.id.btn1);
        RadioGroup radiogroup = (RadioGroup) container.findViewById(R.id.radiogroup);
        // layout params to use when adding each radio button
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        // add 20 radio buttons to the group
        for (int u = 0; u < count_stops; u++) {
            RadioButton newRadioButton = new RadioButton(cntxt);
            String label = Stops[u];
            newRadioButton.setText(label);
            newRadioButton.setId(u);
            radiogroup.addView(newRadioButton, layoutParams);--->crash here line 83
        }
        next_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup radiogroup = (RadioGroup) container.findViewById(R.id.radiogroup);
                int a = radiogroup.getCheckedRadioButtonId();
                //String ID = (String) id_input.getText().toString();
                /*if (a >= 0) {
                    datum = new PaymentData(source, Stops[a], busNumber, url, ID_details);
                    HTTPConnection1 conn = new HTTPConnection1();
                    conn.execute(datum);
                }*/

            }
        });
        return view;
    }
}

fragment_first.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linlayoutBase"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
        <LinearLayout
            android:id="@+id/linearMain"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:id="@+id/HTTPResult"
                android:text="Result"
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:layout_height="wrap_content" />
            <RadioGroup
                android:id="@+id/radiogroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                >
            </RadioGroup>
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/linlayoutButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        >
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Destination Stop"
            android:layout_weight="1"
            >
        </Button>

    </LinearLayout>
</LinearLayout>

activity_fares.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical" android:padding="0dp"
    android:layout_height="wrap_content" android:gravity="fill_vertical" android:layout_margin="0dp">
    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
        <LinearLayout
            android:layout_width="fill_parent"
            android:orientation="vertical" android:padding="0dp"
            android:layout_height="wrap_content" android:gravity="fill_vertical" android:layout_margin="0dp">
            <TextView
                android:id="@+id/txt"
                android:layout_height="wrap_content"
                android:text="Enter the fare between the stops:"
                android:layout_width="wrap_content"
                android:clickable="false"
                android:hint="Enter the fare between the stops::">
            </TextView>

            <TextView
                android:id="@+id/source_stop"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:clickable="false">
            </TextView>

            <TextView
                android:id="@+id/destination_stop"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:clickable="false">
            </TextView>

            <EditText
                android:id="@+id/fares_input"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:hint="Enter the fare:">
            </EditText>

            <EditText
                android:id="@+id/result"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:hint="Result:">
            </EditText>

            <EditText
                android:id="@+id/HTTP_response"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:hint="HTTP Response:">
            </EditText>
            <FrameLayout
                android:id="@+id/frameLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="0dp"
                android:visibility="gone" >

            </FrameLayout>
            <FrameLayout android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:orientation="horizontal"
                android:gravity="fill_horizontal" android:layout_margin="10dp">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="200dp"
                    android:id = "@+id/btn_next_fare"
                    android:text="Enter the next fare:">
                </Button>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:id = "@+id/btn_more_bus"
                    android:text="Input More Bus:">
                </Button>
            </FrameLayout>
            <FrameLayout android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:orientation="horizontal"
                android:gravity="fill_horizontal" android:layout_margin="10dp">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="100dp"
                    android:id = "@+id/btn_skip_Main_Page"
                    android:text="Skip To Main Page">
                </Button>
            </FrameLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

FaresActivity.java:

myf = new PlusOneFragment(cntxt, count_stops, Stops);
                        frameLayout.setVisibility(View.VISIBLE);
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.add(R.id.frameLayout, myf);
                        transaction.commit();

我有一个 activity- FaresActivity。在里面我想要一个片段 - PlusOneFragment。在该片段中,我想在单选按钮中显示停靠点。 String[] Stops 被传递给片段。但是我遇到空指针崩溃。

2021-03-28 08:45:46.309 30183-30183/com.example.swuljcityconductor E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.swuljcityconductor, PID: 30183
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
        at com.example.swuljcityconductor.PlusOneFragment.onCreateView(PlusOneFragment.java:83)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7124)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
2021-03-28 08:45:46.327 30183-30183/com.example.swuljcityconductor I/Process: Sending signal. PID: 30183 SI

G: 9

您正在使用 container.findViewById - 试图在您的 Fragment 视图将要放入的空容器中查找视图。您想改用 view.findViewById 在您的新容器中查找视图膨胀视图。