0

I'm trying to open web browser when someone clicks on textview but as soon as i run the app i get the Null pointer exception error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference

In the xml file

                  <TextView
            android:id="@+id/btnadfree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/txtfeedback1"
            android:layout_margin="0dp"
            android:background="@drawable/shopping"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:autoLink="web"
            android:typeface="sans"/>

in the java file

txtadfree = (TextView) findViewById(R.id.btnadfree);
      txtadfree.setMovementMethod(LinkMovementMethod.getInstance());
    txtadfree.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW);
            browserIntent.setData(Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
        }
    });
sandy
  • 93
  • 1
  • 8
  • you are calling your textview inside onCreate() method ?, is this a fragment or Activity?, please post all the code from the java file – Gastón Saillén Mar 20 '18 at 15:20
  • 1
    `txtadfree` is null .Make sure `R.id.btnadfree` is part of layout and you are getting view after setting content view . – ADM Mar 20 '18 at 15:24
  • Be warned that if you want a TextView with a key listener or movement method not to be focusable, or if you want a TextView without a key listener or movement method to be focusable, you must call setFocusable(boolean) again after calling this to get the focusability back the way you want it. – Tatsuya Mar 20 '18 at 15:30

0 Answers0