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

huangapple go评论76阅读模式
英文:

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

问题

这是日志中显示的运行应用程序时的情况,我无法理解该怎么做,请告诉我该怎么做。当我运行我的应用程序时,活动只是弹出然后立即关闭。我也没有收到“应用程序无响应”的消息。

--------- 崩溃开始
2020-06-29 11:47:22.592 24843-24843/com.example.passwordgenerator E/AndroidRuntime: 致命异常: 主要
    进程: com.example.passwordgenerator, PID: 24843
    java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.example.passwordgenerator/com.example.passwordgenerator.MainActivity}: java.lang.NullPointerException: 尝试调用虚方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()',它位于空对象上的引用
        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)
     原因: java.lang.NullPointerException: 尝试调用虚方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()',它位于空对象上的引用
        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: 发送信号。 PID: 24843 SIG: 9
崩溃结束

这是我的主活动代码:

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, "密码已生成!", 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, "请输入长度", Toast.LENGTH_SHORT).show();
        }

        String capAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String aplha = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "1234567890";
        String symbols = "!@#$%^&*()-_+={}[];:<>,.?/~";

        String values = "";

        if (check.isChecked()) {
            values += capAlpha;
        }

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

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

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

        Random random = new Random();

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

        for (int i = 0; i < length; i++) {
            password[i] = values.charAt(random.nextInt(values.length()));
        }

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

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 &#39;android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()&#39; 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 &#39;android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()&#39; 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.&lt;init&gt;(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)&#160;
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)&#160;
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)&#160;
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)&#160;
at android.os.Handler.dispatchMessage(Handler.java:112)&#160;
at android.os.Looper.loop(Looper.java:216)&#160;
at android.app.ActivityThread.main(ActivityThread.java:7625)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)&#160;
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, &quot;Password Generated!!&quot;, 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, &quot;Please Type in the length&quot;, Toast.LENGTH_SHORT).show();
}
// declaring characters to be used in our password
String capAlpha = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;
String aplha = &quot;abcdefghijklmnopqrstuvwxyz&quot;;
String numbers = &quot;1234567890&quot;;
String symbols = &quot;!@#$%^&amp;*()-_+={}[];:&lt;&gt;&quot;;
// this string here is our main string which
// combines every strings above
String values = &quot;&quot;;
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 &lt; 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);
}
}

答案1

得分: 0

你之所以收到 NullPointerException 错误,是因为你在 onCreate() 方法之外初始化了 CheckBox 视图。

将代码的这部分放到 onCreate() 方法内,问题就应该解决了。

英文:

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.

huangapple
  • 本文由 发表于 2020年6月29日 14:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/62632371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定