Java E/AndroidRuntime:致命异常:主要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25426087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:22:14  来源:igfitidea点击:

E/AndroidRuntime﹕ FATAL EXCEPTION: main

javaandroidandroid-studio

提问by DominikTV

while I test my app, I get the follow error in the Android-Studio-Consol:

当我测试我的应用程序时,我在 Android-Studio-Consol 中收到以下错误:

08-21 13:56:28.059    9637-9637/net.dominik.genpush E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.dominik.genpush, PID: 9637
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.dominik.genpush/net.dominik.genpush.settings}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access0(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java:1884)
        at net.dominik.genpush.settings.<init>(settings.java:23)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
????????????at android.app.ActivityThread.access0(ActivityThread.java:135)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:136)
????????????at android.app.ActivityThread.main(ActivityThread.java:5001)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:515)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
????????????at dalvik.system.NativeStart.main(Native Method)

Here is the piece of code:

这是一段代码:

public CheckBox checkBox_push;
    public TextView textView_appby;
    public Button button_feedback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        checkBox_push = (CheckBox) findViewById(R.id.checkBox_push);

        textView_appby = (TextView) findViewById(R.id.textView_appby);

        button_feedback = (Button) findViewById(R.id.button_feedback);
    }

    public void onTextClickAppBy(View x)
    {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://www.dominiktv.net"));
        startActivity(intent);
    }

    //Button Status Speichern
    @Override
    public void onPause() {
        super.onPause();
        save(checkBox_push.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
        checkBox_push.setChecked(load());
    }

    private void save(final boolean isChecked) {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("check", isChecked);
        editor.commit();

    }

    private boolean load() {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("check", true);
    }
    public void onCheckboxPush(View b)
    {
        if (checkBox_push.isChecked())
        {
            //PB
            Pushbots.getInstance().setNotifyStatus(true);
            //PB E
            Toast.makeText(getApplicationContext(), "Push-Benachrichtigungen aktiviert",
                    Toast.LENGTH_SHORT).show();
        }
        else
        {
            //PB
            Pushbots.getInstance().setNotifyStatus(false);
            //PB E
            Toast.makeText(getApplicationContext(), "Push-Benachrichtigungen deaktiviert",
                    Toast.LENGTH_SHORT).show();
        }
    }

The app also works with the error, but it is not nice to have an error in one app. I already googled a lot but my java skills are not enough very far and this is my first self-created app

该应用程序也可以处理错误,但在一个应用程序中出现错误并不好。我已经在谷歌上搜索了很多,但我的 Java 技能还远远不够,这是我第一个自己创建的应用程序

采纳答案by laalto

Caused by: java.lang.NullPointerException
    at android.app.Activity.findViewById(Activity.java:1884)
    at net.dominik.genpush.settings.<init>(settings.java:23)

You're calling findViewById()too early when initializing an activity settingsobject, likely a member variable. The code you posted doesn't show that.

findViewById()初始化活动settings对象(可能是成员变量)时调用过早。您发布的代码没有显示。

You can call activity functions really only in onCreate()or later.

您实际上只能在中onCreate()或以后调用活动函数。

Also put the findViewById()after setContentView()so it can actually return something other than null.

也把findViewById()aftersetContentView()这样它实际上可以返回 null 以外的东西。

回答by Wesley

NullPointerExceptionin your settings.java, line 23.

NullPointerException在您的settings.java第 23 行。

You can locate your problem code in Cause byof your logcat. Clearly, your problem lies

您可以在Cause bylogcat 中找到问题代码。显然,你的问题在于

at net.dominik.genpush.settings.<init>(settings.java:23)