简体   繁体   English

无法从 EditText 获取文本

[英]can't get a text from EditText

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' java.lang.NullPointerException:尝试调用虚拟方法'android.text.Editable android.widget.EditText.getText()'

Here is my XML:这是我的 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:这是我的 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我无法收到短信,请告诉我此代码有什么问题

If you just want to display text, please heed Darkman's advice ie use TextView.如果您只想显示文本,请听从 Darkman 的建议,即使用 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.请不要将您的 TextView 命名为btn显示,因为它们非常具有误导性。

I'm assuming that you want to:我假设你想要:

  • click down_btn TextView and trigger method videoDownload点击down_btn TextView和触发方法视频下载

  • videoDownload will display a toast message then... videoDownload 将显示一条 toast 消息,然后...

  • Text typed in EditText to show on show TextView.在 EditText 中键入的文本以显示在 TextView 上。

I tried your code on emulator and it's working.我在模拟器上试过你的代码,它正在工作。 The text from EditText is displayed in the "show" Textview. EditText 中的文本显示在“show”Textview 中。

Please check your other xml attributes.请检查您的其他 xml 属性。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM