-1

Actually I saw a yt video of how to intent fragment and acticity and that is what I tried but don"t know how the error is coming.The error which I am having is ** java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.photoframeapp.HomeFragment.onCreateView(HomeFragment.java:31)**.I tried to sort out but I am not able to .

this is my HomeFragment.java class

public class HomeFragment extends Fragment {

   TextView beginners;

    public HomeFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        beginners.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),mainpage.class));
            }
        });

        return view;
    }
}

this is my mainpage.java file

 public class mainpage extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainpage);
    
    
    
        }
    
    
    }

fragment_home.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="387dp"
        android:alpha="0.4"
        android:background="@color/Purple" />

    <TextView
        android:id="@+id/beginners"
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="-27dp"
        android:layout_marginBottom="433dp"
        android:freezesText="true"
        android:text="     BEGINNERS CALLIGRAPHY"
        android:textSize="25dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="195dp"
        android:alpha="0.4"
        android:background="@color/DeepPink" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="-9dp"
        android:layout_marginBottom="67dp"
        android:text="     ADVANCED CALLIGRAPHY"
        android:textSize="25dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="4dp"
        android:alpha="0.4"
        android:background="@color/DarkBlue" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="-8dp"
        android:layout_marginBottom="261dp"
        android:freezesText="true"
        android:text=" INTERMEDIATE CALLIGRAPHY"
        android:textSize="25dp" />

</RelativeLayout>
sank
  • 133
  • 7

1 Answers1

0

You have to initialize textView before using it.

View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners = view.findViewById(R.id.beginners) 
beginners.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getActivity(),mainpage.class));
                    }
                });
Kishan Maurya
  • 3,356
  • 8
  • 21