edittext问题

时间:2015-10-10 22:09:57

标签: android

我是java的新手,我在转换"输入"字符串。

input = (EditText) findViewById(R.id.wpiszZapytanie);
String zapytanieDoUrl = input.getText().toString();

它崩溃了这个日志:

10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.qm.mood/eu.qm.mood.GIFActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:172)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5832)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at eu.qm.mood.GIFActivity.onCreate(GIFActivity.java:18)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6221)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:172) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5832) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
10-10 23:57:48.589 9423-9423/eu.qm.mood E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

正如我所看到的,某处没有(null)。我理解这段代码无法查看文本框中的内容,用户输入的内容。

Edittext位于content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView android:text="@string/welcome" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="@color/colorAccent"
        android:textStyle="bold"
        android:fontFamily="Roboto"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/wpiszZapytanie"
        android:singleLine="true"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/go"
        android:id="@+id/doit"
        android:onClick="goClick"
        android:layout_below="@+id/wpiszZapytanie"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

这里是activity_gif.xml

<?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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context="eu.qm.mood.GIFActivity">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/GifResulttwo"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/editText" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

您的traceId'变量为空,这就是您获得异常的原因。

可能的原因:

  • 我认为您对EditText使用了错误的input。如果id元素的标识为EditText
  • ,请检查您的xml
  • 您可能正在虚增错误的xml布局,该布局没有wpiszZapytanie,内容为EditText
  • 正如@ {Pyrodante所指出的那样wpiszZapytanie之前您可能正在findViewById(R.id.wpiszZapytanie);

答案 1 :(得分:0)

正如pgitu所指出的:input为null,当findViewById看起来无法找到它正在寻找的内容时会发生这种情况。其中一个主要原因是在调用setContentView之前调用它。

在设置UI之前,您无法访问UI元素。所以,如果上述两个不是它,也许就是这样。

答案 2 :(得分:0)

确保您的GIFActivity在onCreate方法上遵循此逻辑。

public class GIFActivity extends Activity {
EditText input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); //First
        setContentView(R.layout.activity_gif);//Second
        input = (EditText) findViewById(R.id.wpiszZapytanie);//Third

        String text = input.getText().toString();
    }   
}

activity_gif.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <EditText 
            android:id="@+id/wpiszZapytanie"/>    

    </LinearLayout>