Android小部件崩溃[重复]

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

我一直在尝试初始化UI窗口小部件,但它正在获取错误,我在互联网上查找了问题,但无法为自己的代码解决。该错误可能是由于与范围相关的某些问题而出现的,但我无法识别我的错误。

请帮帮我!非常感谢!

错误日志:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.getroasted/com.example.android.getroasted.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference

片段文件

public class HomeFragment extends Fragment implements View.OnClickListener {

    private static final int GALLERY_REQUEST_CODE = 1;
    private static final int REQUEST_IMAGE_CAPTURE = 1;

    private HomeViewModel homeViewModel;

    private ImageView clickPhoto;
    private ImageView addPhoto;
    private ImageView photo;
    private Button savePhotoButton;
    private TextView roastText;

    FirebaseFirestore db;
    FirebaseStorage storage;
    StorageReference storageRef;
    String mRoastText;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        storage = FirebaseStorage.getInstance();
        storageRef = storage.getReference();
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        roastText.findViewById(R.id.roastText);
        mRoastText = roastText.getText().toString();
        clickPhoto.findViewById(R.id.clickPhoto).setOnClickListener(this);
        addPhoto.findViewById(R.id.clickPhoto).setOnClickListener(this);
        savePhotoButton.findViewById(R.id.savePhotoButton).setOnClickListener(this);
    }

片段XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".ui.home.HomeFragment">

    <ImageView
        android:id="@+id/clickPhoto"
        android:layout_width="69dp"
        android:layout_height="76dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="113dp"
        android:layout_marginEnd="229dp"
        android:layout_marginBottom="661dp"
        android:src="@drawable/ic_add_a_photo_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.25"
        app:layout_constraintRight_toLeftOf="@+id/addPhoto"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <ImageView
        android:id="@+id/addPhoto"
        android:layout_width="69dp"
        android:layout_height="76dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="108dp"
        android:layout_marginBottom="662dp"
        android:src="@drawable/ic_add_to_photos_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.701"
        app:layout_constraintLeft_toRightOf="@+id/clickPhoto"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <ImageView
        android:id="@+id/photo"
        android:layout_width="250dp"
        android:layout_height="633dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="77dp"
        android:layout_marginEnd="77dp"
        android:layout_marginBottom="100dp"
        android:src="@drawable/image"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.669"></ImageView>

    <Button
        android:id="@+id/savePhotoButton"
        android:layout_width="68dp"
        android:layout_height="78dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="343dp"
        android:layout_marginBottom="653dp"
        android:background="@drawable/mybutton"
        android:text="Save the Roast!"
        android:textSize="25sp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.585"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1" />
<!--TODO:
        * Add a font
        * Change size
        * Add background
 -->
    <TextView
        android:id="@+id/roastText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="177dp"
        android:layout_marginBottom="250dp"
        android:text="Roast"
        android:textSize="35sp"
        android:visibility="gone"/>


</RelativeLayout>
java android xml firebase android-studio
1个回答
0
投票

您正在编写的文本视图roasrText在TextView中具有

onViewCreated中的view参数是onCreateView中膨胀的片段视图,因此请使用该view参数查找您的视图

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        roastText = view.findViewById(R.id.roastText);
...
© www.soinside.com 2019 - 2024. All rights reserved.