E / AndroidRuntime: FATAL EXCEPTION: main

while i'm testing my app i get the following error in 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.access$800(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.access$800(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's a snippet 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 fails, but it's nice to have the error in one app. I've googled a lot already but my java skills are not enough and this is my first self-made app.

+3


source to share


2 answers


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

      

You are calling findViewById()

too early when you initialize the activity object settings

, probably a member variable. The code you posted doesn't show this.



You can only really call activity functions at onCreate()

or later.

Also put findViewById()

after setContentView()

so that it can actually return something other than zero.

+3


source


NullPointerException

on line settings.java

, line 23.

You can find the problem code in Cause by

your logarithm. Obviously your problem is



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

      

0


source







All Articles