Umer Aziz
Umer Aziz

Reputation: 111

can't get a text from EditText

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()'

Here is my XML :

<EditText
   android:id="@+id/linkvideo"
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:background="@drawable/et_back"
   android:hint="@string/paste"
   android:padding="15dp"
   android:textColor="@color/black"
   android:textColorHint="@color/grey"
   android:textSize="15sp"
   android:inputType="text"/>

Here is my java :

 public class MainActivity extends AppCompatActivity {
    
        EditText inputurl;
        String newLink;
        TextView down_btn ,show;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            inputurl=(EditText)findViewById(R.id.linkvideo);
            down_btn=findViewById(R.id.down_btn);
            show=findViewById(R.id.show);
    
            down_btn.setOnClickListener(this::videoDownload);
    
    
        }
        public void videoDownload(View view) {
            Toast.makeText(MainActivity.this, "clicking ....", Toast.LENGTH_SHORT).show();
            String ans=inputurl.getText().toString();
            show.setText(ans);
        }
    
    }

I can't get a text please tell me what's wrong with this Code

Upvotes: 0

Views: 93

Answers (1)

livenlearnaday
livenlearnaday

Reputation: 161

If you just want to display text, please heed Darkman's advice i.e. use TextView.

It is not clear what you are trying to achieve.

Please don't name your TextView as btn and show as they are very misleading.

I'm assuming that you want to:

  • click down_btn TextView and trigger method videoDownload

  • videoDownload will display a toast message then...

  • Text typed in EditText to show on show TextView.

I tried your code on emulator and it's working. The text from EditText is displayed in the "show" Textview.

Please check your other xml attributes.

Here is what I'm using. I replaced some of the attributes and apply no design.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <EditText

        android:id="@+id/linkvideo"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:textColorHint="@color/white"
        android:hint="Some hint"
        android:padding="15dp"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="15sp"
        android:inputType="text"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/down_btn"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />


    <TextView
        android:id="@+id/down_btn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:text="CLICK HERE"
        android:gravity="center"
        app:layout_constraintTop_toBottomOf="@id/linkvideo"
        app:layout_constraintBottom_toTopOf="@id/show"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
     />


    <TextView
        android:id="@+id/show"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@color/purple_200"
        android:textStyle="bold"
        android:text="TextView for text in EditText to display when click: CLICK HERE "
        app:layout_constraintTop_toBottomOf="@id/down_btn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />





</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Related Questions