简体   繁体   中英

I have this crash while testing my app. I am a complete beginner and cant understand how to fix this, any solution?

This is what the logcat is showing Me while Running the app, i cant understand what to do, please help me by telling me what to do. when i run my app the activity just pops up and closes in an instant. i dont get a 'app not responding' message too

--------- beginning of crash
2020-06-29 11:47:22.592 24843-24843/com.example.passwordgenerator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.passwordgenerator, PID: 24843
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.passwordgenerator/com.example.passwordgenerator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3355)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at android.content.Context.obtainStyledAttributes(Context.java:679)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
        at com.example.passwordgenerator.MainActivity.<init>(MainActivity.java:16)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1224)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3340)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
2020-06-29 11:47:22.638 24843-24843/? I/Process: Sending signal. PID: 24843 SIG: 9

My Java code is comepletely error free and all the warnings have been fixed.. i did not tweak anything that i think could have caused this error

this is my main activity code here

package com.example.passwordgenerator;

import androidx.appcompat.app.AppCompatActivity;



import android.os.Bundle;

import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    CheckBox check = findViewById(R.id.CheckBox);
    CheckBox check1 = findViewById(R.id.CheckBox1);
    CheckBox check2 = findViewById(R.id.CheckBox2);
    CheckBox check3 = findViewById(R.id.CheckBox3);
    public void Password(View view) {
        Toast.makeText(this, "Password Generated!!", Toast.LENGTH_SHORT).show();
        EditText editText = findViewById(R.id.editText);
        EditText editText1 = findViewById(R.id.editText2);
        int length = Integer.parseInt(editText.getText().toString());
        if (length==0){
            Toast.makeText(this, "Please Type in the length", Toast.LENGTH_SHORT).show();
        }
        // declaring characters to be used in our password
        String capAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String aplha = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "1234567890";
        String symbols = "!@#$%^&*()-_+={}[];:<>";

        // this string here is our main string which
        // combines every strings above
        String values = "";
        if (check.isChecked()) {
            values += capAlpha;
        }

        if (check1.isChecked()) {
            values += numbers;
        }

        if (check2.isChecked()) {
            values += aplha;
        }

        if (check3.isChecked()) {
            values += symbols;
        }

        // Using random method
        Random random = new Random();

        char[] password;
        password = new char[length];

        for (int i = 0; i < length; i++)
        {
            // Use of charAt() method : to get character value
            // Use of nextInt() as it is scanning the value as int
            password[i] = values.charAt(random.nextInt(values.length()));

        }
        String Password = String. valueOf(password);
        editText1.setText(Password);


    }
}

You're getting NullPointerException because you're initializing the CheckBox views outside the onCreate() method.

CheckBox check = findViewById(R.id.CheckBox);
CheckBox check1 = findViewById(R.id.CheckBox1);
CheckBox check2 = findViewById(R.id.CheckBox2);
CheckBox check3 = findViewById(R.id.CheckBox3);

Keep this portion of the code inside onCreate() method and that should solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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