JavaNullPointer用于带有按钮的片段

时间:2019-05-17 14:27:04

标签: java android button onclicklistener

我是新近使用的编码器,开始为大学创建电视应用程序,但遇到了我一直苦苦挣扎的错误。我有一个片段(在Android Studio中),在几天前就可以正常工作了,但是当我上次运行该程序时是在单击“显示片段”按钮后立即关闭应用程序的。而且我不明白为什么会有NullPointerException

ShowsFragment.java

   public class ShowsFragment extends Fragment {

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

            Button DataClickButton;

            View v = inflater.inflate(R.layout.activity_add_shows_activity, container, false);

            DataClickButton = v.findViewById(R.id.Data_button);
            DataClickButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startShowsActivity();
                }
            });

            return v;
        }

        public void startShowsActivity() {

            Intent showsIntent = new Intent(getActivity(), AddShowsActivity.class);
            startActivity(showsIntent);
        }
    }     

fragment_shows.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/Data_button"
        android:layout_width="167dp"
        android:layout_height="67dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="122dp"
        android:layout_marginLeft="122dp"
        android:layout_marginTop="293dp"
        android:onClick="DataClickButton"
        android:text="@string/add_shows"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Stack Trace

0 个答案:

没有答案