Android Studio 未显示错误,但应用程序崩溃了

问题描述 投票:0回答:1

我正在我的应用程序的一个片段中制作一个 gpa 计算器。计算器打开后会使应用程序崩溃。

请帮忙,可能有很多错误,我是java和android的初学者。这是我的代码:

public class WavgFragment extends Fragment {

    Activity activity = getActivity();




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        EditText ela = activity.findViewById(R.id.subela);
        EditText math =  activity.findViewById(R.id.submath);
        EditText sci =  activity.findViewById(R.id.subsci);
        EditText ss =  activity.findViewById(R.id.subss);
        EditText wl =  activity.findViewById(R.id.subwl);
        Button calc =  activity.findViewById(R.id.calc);
        TextView gpa =  activity.findViewById(R.id.gpa);

        float num1 = Float.parseFloat(ela.getText().toString());
        float num2 = Float.parseFloat(math.getText().toString());
        float num3 = Float.parseFloat(sci.getText().toString());
        float num4 = Float.parseFloat(ss.getText().toString());
        float num5 = Float.parseFloat(wl.getText().toString());

        calc.setOnClickListener(v -> {
            float sum = num1 + num2 + num3 + num4 + num5;

            float avg = sum/5;

            String avg1 = Float.toString(avg);

            gpa.setText(avg1);

        });
        return inflater.inflate(R.layout.fragment_wavg, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

    }
}

还有很多东西要补充,这仍然是基本的,这是我的logcat:

FATAL EXCEPTION: main
Process: com.coding.studentsparks, PID: 20080
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
    at com.coding.studentsparks.WavgFragment.onCreateView(WavgFragment.java:33)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:205)
    at android.os.Looper.loop(Looper.java:294)
    at android.app.ActivityThread.main(ActivityThread.java:8248)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)                                                                                           
java android android-fragments crash
1个回答
0
投票

由于您是 android studio 和 java 的新手,因此您不应该将所有代码编写在

onCreateView
中,而应该将所有代码编写在
onViewCreated
中。 现在继续处理您的错误,发生这种情况是因为您正在使用
EditText ela = activity.findViewById(R.id.subela);

如果您在 onCreateView 中编写代码,您应该按如下方式执行:

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

    View v = inflater.inflate(R.layout.fragment_wavg, container, false);
    EditText ela = v.findViewById(R.id.subela);

    // rest of your code. at the end return the view.
    return v;

}

我希望这能解决您的问题。祝您编码愉快。

© www.soinside.com 2019 - 2024. All rights reserved.