Android 应用程序在 RatingBar DialogFragment 关闭后崩溃

Android app crashes after RatingBar DialogFragment closes

我的应用程序的目的是通过自定义对话框从评级栏中检索一个值(满分 5),并将其显示在主 activity 的 TextView 中。当我点击下图中用红色勾勒出的按钮时,应用程序崩溃并关闭。

该应用程序包含 2 个 classes/activities。主 activity 和自定义对话框 activity.

两个文件的代码如下:

MainActivity.java:

package com.example.mealrater;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements MealRaterDialog.SaveRating {

    public EditText restaurant, dish;
    public TextView ratingDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        restaurant = findViewById(R.id.etRestaurant);
        dish = findViewById(R.id.etDish);

        RateMealButton();

    }

    @Override
    public void finishMealRaterDialog(String rating) {
        ratingDisplay = findViewById(R.id.tvRatingDisplay);
        ratingDisplay.setText(rating);
    }

    private void RateMealButton() {

        Button rate = findViewById(R.id.btnRate);
        rate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                FragmentManager fm = getSupportFragmentManager();
                MealRaterDialog mealRaterDialog = new MealRaterDialog();
                mealRaterDialog.show(fm, "RateMeal");

            }
        });
    }
}

MealRaterDialog.java:

package com.example.mealrater;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import androidx.fragment.app.DialogFragment;

public class MealRaterDialog extends DialogFragment {

    String rating;

    public interface SaveRating {
        void finishMealRaterDialog(String rating);
    }

    public MealRaterDialog() {
    }

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

        final View view = inflater.inflate(R.layout.meal_rater, container);
        getDialog().setTitle("Rate your meal");

        Button save = view.findViewById(R.id.btnSaveRating);
        save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                RatingBar ratingBar = view.findViewById(R.id.rbMeal);
                rating = String.valueOf(ratingBar.getRating());
                SaveItem(String.valueOf(rating));

            }
        });
        return view;
    }

    private void SaveItem(String rating) {

        MealRaterDialog.SaveRating activity = (MealRaterDialog.SaveRating) getActivity();
        activity.finishMealRaterDialog(rating);
        getDialog().dismiss();

    }
}

我是 Android Studio 的新手,也想收到有关如何改进此问题的提示。谢谢。

Logcat错误:

2021-04-28 16:04:35.240 18144-18144/com.example.mealrater E/libc: Access denied finding property "ro.serialno"
2021-04-28 16:04:42.709 18144-18144/com.example.mealrater E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mealrater, PID: 18144
    java.lang.NullPointerException: Attempt to invoke virtual method 'float android.widget.RatingBar.getRating()' on a null object reference
        at com.example.mealrater.MealRaterDialog.onClick(MealRaterDialog.java:35)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access00(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

对此有几个修复。

首先,您应该在 onClick 之前定义 ratingBar 小部件。现在,每次单击按钮时它都会创建变量。在 Button save = view.findViewById(R.id.btnSaveRating); 行之后定义它。

其次,当您使用对话框片段时,点击的所有逻辑都应该在名为 onViewCreated() 的覆盖函数上调用。不像你现在正在做的那样在 OnCreateView() 中。让我知道它是否适合你。

我已尝试重现此错误,并找到适合我的解决方案。 您必须使用视图(在侦听器之外)创建评级栏。

这应该是您 MealRaterDialog.java class

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

        final View view = inflater.inflate(R.layout.meal_rater, container);
        getDialog().setTitle("Rate your meal");

        Button save = view.findViewById(R.id.btnSaveRating);
        RatingBar ratingBar = view.findViewById(R.id.rbMeal);
        save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                rating = String.valueOf(ratingBar.getRating());
                SaveItem(String.valueOf(rating));

            }
        });
        return view;
    }

希望有用!