简体   繁体   English

Android 应用程序强制关闭错误

[英]Force Close error on Android Application

I have added the Results class to the Android Manifest, so yes, you can cross that off the list.我已将结果 class 添加到 Android 清单中,所以是的,您可以将其从列表中删除。 Is it an error in the way I've set out the method?我设置方法的方式有误吗?

    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);
        }



    }

When I start this class, it always seems to force close my app.当我启动这个 class 时,它似乎总是强制关闭我的应用程序。 Can anyone tell me what I'm doing wrong?谁能告诉我我做错了什么?

Here's the log cat:这是日志猫:

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

You are getting a NullPointerException somewhere in your OnCreate.您在 OnCreate 中的某处收到NullPointerException I am guessing that either the ids are wrong in your rslt.xml layout so result and fbFriends is null, or fbFriends doesn't have any text in it so getText() returns null.我猜测要么是您的 rslt.xml 布局中的 id 错误,因此结果和 fbFriends 是 null,要么是 fbFriends 中没有任何文本,因此getText()返回 null。

It's a null pointer exception in onCreate, so why not try stepping through onCreate to see what variable is null?是onCreate中的null指针异常,那么为什么不尝试单执行onCreate看看null是什么变量呢?

That said, I'm guessing R.id.noFacebookFriends is not part of the R.layout.rslt .也就是说,我猜R.id.noFacebookFriends不是R.layout.rslt的一部分。

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

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