无法从 editText 获取文本

发布于 2024-12-15 18:18:01 字数 3864 浏览 0 评论 0原文

    public class screen2 extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen2);

            final Button button1 = (Button)findViewById(R.id.button1);        
            button1.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                    //AInteger.parseInt(string)
                    System.out.println("1");
                    EditText et = (EditText)findViewById(R.id.editText1);
                    System.out.println("2");
                    int zipCode =  Integer.parseInt(et.getText().toString());
                    System.out.println("3");
                    System.out.println(zipCode);

                    System.out.println("dude...5");
                 }
            });
        }        
   }

这段代码显示了 logcat 中的错误:

    11-13 19:58:06.806: W/KeyCharacterMap(281): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-13 19:58:09.471: I/System.out(281): 1
11-13 19:58:09.471: I/System.out(281): 2
11-13 19:58:09.477: D/AndroidRuntime(281): Shutting down VM
11-13 19:58:09.477: W/dalvikvm(281): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-13 19:58:09.527: E/AndroidRuntime(281): FATAL EXCEPTION: main
11-13 19:58:09.527: E/AndroidRuntime(281): java.lang.NullPointerException
11-13 19:58:09.527: E/AndroidRuntime(281):  at com.example.andtwi.screen2$1.onClick(screen2.java:23)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.view.View.performClick(View.java:2408)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.view.View$PerformClick.run(View.java:8816)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.os.Handler.handleCallback(Handler.java:587)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.os.Looper.loop(Looper.java:123)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 19:58:09.527: E/AndroidRuntime(281):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:58:09.527: E/AndroidRuntime(281):  at java.lang.reflect.Method.invoke(Method.java:521)
11-13 19:58:09.527: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 19:58:09.527: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 19:58:09.527: E/AndroidRuntime(281):  at dalvik.system.NativeStart.main(Native Method)

虽然它看起来像空指针异常,但我认为我正在做的事情正是我在论坛上找到的。这也是我的相关 xml 文件的代码。谁能建议我哪里做错了?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Zipcode" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="5" >

        <requestFocus />
    </EditText>

    <Button
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get movies from Flixster" />

</LinearLayout>

代码也在此中断:

int zipCode =  Integer.parseInt(et.getText().toString());
    public class screen2 extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen2);

            final Button button1 = (Button)findViewById(R.id.button1);        
            button1.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                    //AInteger.parseInt(string)
                    System.out.println("1");
                    EditText et = (EditText)findViewById(R.id.editText1);
                    System.out.println("2");
                    int zipCode =  Integer.parseInt(et.getText().toString());
                    System.out.println("3");
                    System.out.println(zipCode);

                    System.out.println("dude...5");
                 }
            });
        }        
   }

This code shows the errror in the logcat :

    11-13 19:58:06.806: W/KeyCharacterMap(281): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-13 19:58:09.471: I/System.out(281): 1
11-13 19:58:09.471: I/System.out(281): 2
11-13 19:58:09.477: D/AndroidRuntime(281): Shutting down VM
11-13 19:58:09.477: W/dalvikvm(281): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-13 19:58:09.527: E/AndroidRuntime(281): FATAL EXCEPTION: main
11-13 19:58:09.527: E/AndroidRuntime(281): java.lang.NullPointerException
11-13 19:58:09.527: E/AndroidRuntime(281):  at com.example.andtwi.screen2$1.onClick(screen2.java:23)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.view.View.performClick(View.java:2408)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.view.View$PerformClick.run(View.java:8816)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.os.Handler.handleCallback(Handler.java:587)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.os.Looper.loop(Looper.java:123)
11-13 19:58:09.527: E/AndroidRuntime(281):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 19:58:09.527: E/AndroidRuntime(281):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:58:09.527: E/AndroidRuntime(281):  at java.lang.reflect.Method.invoke(Method.java:521)
11-13 19:58:09.527: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 19:58:09.527: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 19:58:09.527: E/AndroidRuntime(281):  at dalvik.system.NativeStart.main(Native Method)

Though it looks like an Null Pointer exception , i think i am doing it things exactly i found on forums. Here is code of my related xml file as well. Can anyone suggest where am i doing it wrong?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Zipcode" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="5" >

        <requestFocus />
    </EditText>

    <Button
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get movies from Flixster" />

</LinearLayout>

The code breaks at this as well :

int zipCode =  Integer.parseInt(et.getText().toString());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-12-22 18:18:01

试试这个:

Button button1 = (Button)findViewById(R.id.button1);  
EditText et = (EditText)findViewById(R.id.editText1);

button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                System.out.println("1");                
                System.out.println("2");
                String zipCode = et.getText().toString();
                System.out.println("3");
                System.out.println(zipCode); 
            }
}

我想,你不需要在那里写 String zipCode = (String)et.getText().toString(); 因为它已经返回了 String 对象。

希望您不要错过在声明 ButtonEditText 之前包含 setContentView(R.layout.your_xml_file);

编辑1:

您似乎没有遇到

int zipCode=Integer.parseInt(et.getText().toString()); 的问题。尝试上面的方法代码修改。

您可能会遇到空指针异常,因为您将 String s=et.getText().toString() 设为 null。请在将其转换为 int 之前检查它是否为 null 。

按钮button1 = (Button)findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);

button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                System.out.println("1");                
                System.out.println("2");
                String check = et.getText().toString();
                int zipCode=0;
                if(!check.equals(""))
                    zipCode=Integer.parseInt(check);
                System.out.println("3");
                System.out.println(zipCode); 
            }
}

Try this:

Button button1 = (Button)findViewById(R.id.button1);  
EditText et = (EditText)findViewById(R.id.editText1);

button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                System.out.println("1");                
                System.out.println("2");
                String zipCode = et.getText().toString();
                System.out.println("3");
                System.out.println(zipCode); 
            }
}

I think,you don't need to write String zipCode = (String)et.getText().toString(); there as it is already returns a String object.

And hope,you don't miss to include setContentView(R.layout.your_xml_file); before declaring a Button and EditText.

Edit 1:

You don't seem to get problem with

int zipCode=Integer.parseInt(et.getText().toString()); there.Try this with above code modification.

You might be getting null pointer exception because you are getting String s=et.getText().toString() as null.Please check it for null before casting it to int.

Button button1 = (Button)findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);

button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                System.out.println("1");                
                System.out.println("2");
                String check = et.getText().toString();
                int zipCode=0;
                if(!check.equals(""))
                    zipCode=Integer.parseInt(check);
                System.out.println("3");
                System.out.println(zipCode); 
            }
}
猫瑾少女 2024-12-22 18:18:01

您可以单独发布您的 onCreate 方法以及第 23 行吗?最可能的问题是您忽略调用 setContentView,这就是 findViewById 返回 null 的原因。

Could you post your onCreate method, as well as line 23 on its own? The most likely problem is that you neglect to call setContentView and that is why findViewById returns null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文