1

我已将 Results 类添加到 Android Manifest,所以是的,您可以将其从列表中删除。我制定方法的方式有误吗?

    package com.kenning.foreveralone;


    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.EditText;
    import android.widget.TextView;

    public class Results extends Activity{

        EditText fbFriends;
        TextView resulting;
        String value;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.rslt);
            TextView resulting = (TextView) findViewById(R.id.result);
            EditText fbFriends = (EditText) findViewById(R.id.noFacebookFriends);
            String value = fbFriends.getText().toString();
            resulting.setText("Your score is " + value);
        }



    }

当我开始这门课时,它似乎总是强制关闭我的应用程序。谁能告诉我我做错了什么?

这是日志猫:

04-08 20:05:24.169: E/AndroidRuntime(589): FATAL EXCEPTION: main
04-08 20:05:24.169: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kenning.foreveralone/com.kenning.foreveralone.Results}: java.lang.NullPointerException
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.os.Looper.loop(Looper.java:123)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-08 20:05:24.169: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
04-08 20:05:24.169: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:521)
04-08 20:05:24.169: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-08 20:05:24.169: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-08 20:05:24.169: E/AndroidRuntime(589):  at dalvik.system.NativeStart.main(Native Method)
04-08 20:05:24.169: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
04-08 20:05:24.169: E/AndroidRuntime(589):  at com.kenning.foreveralone.Results.onCreate(Results.java:20)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-08 20:05:24.169: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-08 20:05:24.169: E/AndroidRuntime(589):  ... 11 more
4

2 回答 2

0

NullPointerException您在 OnCreate中获得了某个位置。我猜要么你的 rslt.xml 布局中的 id 错误,所以结果和 fbFriends 为空,或者 fbFriends 中没有任何文本,所以getText()返回空。

于 2012-04-08T19:27:27.287 回答
0

这是 onCreate 中的空指针异常,那么为什么不尝试单执行以onCreate查看哪个变量为空呢?

也就是说,我猜R.id.noFacebookFriends不是R.layout.rslt.

于 2012-04-08T19:29:22.730 回答