gpt4 book ai didi

java - 测试我的应用程序时出现此崩溃。我是一个完整的初学者,无法理解如何解决此问题,任何解决方案?

转载 作者:行者123 更新时间:2023-12-02 07:29:53 25 4
gpt4 key购买 nike

这是运行应用程序时logcat向我显示的内容,
我不明白该怎么做,请告诉我该怎么做来帮助我。
当我运行我的应用程序时, Activity 会立即弹出并关闭。
我也没有收到“应用未响应”消息

--------- 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
我的Java代码完全没有错误,并且所有警告已修复..
我没有调整我认为可能导致此错误的任何内容
这是我的主要 Activity 代码
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);


}
}

最佳答案

之所以得到NullPointerException,是因为您正在CheckBox方法之外初始化onCreate() View 。

CheckBox check = findViewById(R.id.CheckBox);
CheckBox check1 = findViewById(R.id.CheckBox1);
CheckBox check2 = findViewById(R.id.CheckBox2);
CheckBox check3 = findViewById(R.id.CheckBox3);
将这部分代码保留在 onCreate()方法内,应该可以解决问题。

关于java - 测试我的应用程序时出现此崩溃。我是一个完整的初学者,无法理解如何解决此问题,任何解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62632371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com