尝试将值添加到TextView Android时java.lang.NullPointerException

时间:2019-03-10 19:12:46

标签: android

我收到此错误:

  

E / Android运行时:致命异常:主要   流程:com.example.androidproyectofinal,PID:14575   java.lang.NullPointerException:尝试在虚拟设备上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'   空对象引用           在com.example.androidproyectofinal.Peliculas.Psicosis $ 1.onClick(Psicosis.java:86)

我尝试了所有看到的解决方案,但无法解决。

这是带有错误的Fragment Java代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_psicosis, container, false);
    img = (ImageView)view.findViewById(R.id.imageView4);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            text = view.findViewById(R.id.textView7);
            text.setText("Psicosis");
            text1 = view.findViewById(R.id.textView8);
            text1.setText("Es un Thriller creado por Alfred Hitchcock");
            img1= view.findViewById((R.id.imageView4));
            img1.setImageResource(R.drawable.psicosis);
            pelicula = (MainPeliculas) getActivity();
            //Below is where you get a variable from the main activity
            pelicula.textview = text;
            pelicula.textview2 = text1;
            pelicula.imageView = img1;
            Intent intent = new Intent(getActivity(), MainPeliculas.class);

            startActivity(intent);
        }
    });
    return view;
}

变量在这里:

public class Psicosis extends Fragment {
ImageView img;
TextView text;
TextView text1;
ImageView img1;
public MainPeliculas pelicula;

以及该片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Peliculas.Psicosis">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="383dp"
        android:clickable="true"
        android:src="@drawable/psicosis"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="204dp"
        android:layout_height="61dp"
        android:layout_marginStart="103dp"
        android:layout_marginTop="62dp"
        android:layout_marginEnd="104dp"
        android:layout_marginBottom="26dp"
        android:text="Psicosis"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@android:color/background_dark"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/textView8"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="204dp"
        android:layout_height="61dp"
        android:layout_marginStart="103dp"
        android:layout_marginTop="27dp"
        android:layout_marginEnd="104dp"
        android:layout_marginBottom="138dp"
        android:text="Alfred Hitchcock"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView7" />
</android.support.constraint.ConstraintLayout>

我已经检查了所有id,当他尝试将字符串放入变量文本时会不断收到错误消息,表明该值是null。

1 个答案:

答案 0 :(得分:-1)

隐藏名称有问题。

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

    // NAME THIS VARIABLE DIFFERENTLY
    View inflatedView = inflater.inflate(R.layout.fragment_psicosis, container, false);
    img = (ImageView) view.findViewById(R.id.imageView4);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // USE HERE NEW NAME "inflatedView"
            text = inflatedView.findViewById(R.id.textView7);
            text.setText("Psicosis");


            // USE HERE NEW NAME "inflatedView"
            text1 = inflatedView.findViewById(R.id.textView8);
            text1.setText("Es un Thriller creado por Alfred Hitchcock");


            // USE HERE NEW NAME "inflatedView"
            img1 = inflatedView.findViewById((R.id.imageView4));
            img1.setImageResource(R.drawable.psicosis);

            pelicula = (MainPeliculas) getActivity();
            //Below is where you get a variable from the main activity
            pelicula.textview = text;
            pelicula.textview2 = text1;
            pelicula.imageView = img1;
            Intent intent = new Intent(getActivity(), MainPeliculas.class);

            startActivity(intent);
        }
    });

    return view;
}

在您的代码中,您拥有view的身份:

  • 方法参数(在onClick(View view)中)

  • 布局夸大(在View inflatedView = inflater.inflate(...)中)