gpt4 book ai didi

java - 我的应用程序在 android studio 中崩溃了,在查看日志时我无法弄清楚出了什么问题

转载 作者:行者123 更新时间:2023-12-01 07:52:14 25 4
gpt4 key购买 nike

所以我试图在 Android Studio 中构建一个登录应用程序,当我测试它时,该应用程序由于某种原因崩溃了。我只能通过查看日志来找出问题,但我无法针对此异常错误执行此操作。下面粘贴的是我的日志。

02-09 16:18:09.482 2467-2467/? I/art: Not late-enabling -Xcheck:jni         (already on)
02-09 16:18:09.585 2467-2467/edu.dtcc.bwharto9.loginform W/System: ClassLoader referenced unknown path: /data/app/edu.dtcc.bwharto9.loginform-2/lib/x86
02-09 16:18:09.590 2467-2467/edu.dtcc.bwharto9.loginform D/AndroidRuntime: Shutting down VM
02-09 16:18:09.590 2467-2467/edu.dtcc.bwharto9.loginform E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.dtcc.bwharto9.loginform, PID: 2467
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.dtcc.bwharto9.loginform/edu.dtcc.bwharto9.loginform.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at edu.dtcc.bwharto9.loginform.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) `

这也是我的 Java

    package edu.dtcc.bwharto9.loginform;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {'

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

EditText userName = (EditText)findViewById(R.id.userName);
EditText password = (EditText)findViewById(R.id.password);
public void login() {
if (userName.getText().toString().equals("student") && password.getText().toString().equals("password"))
{
Toast.makeText(getApplicationContext(), "You have sucessfully logged in!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Login error! Please check your username or password!", Toast.LENGTH_LONG).show();
}
}

}

XML

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.dtcc.bwharto9.loginform.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please enter your username and Password "
android:id="@+id/instructionsText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/userName"
android:layout_below="@+id/instructionsText"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:hint="Username" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="Password"
android:password="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="@+id/btnLogIn"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:onClick="login" />
</RelativeLayout>

感谢所有帮助我的人,我已经完全康复了!

最佳答案

EditText userName = (EditText)findViewById(R.id.userName);
EditText password = (EditText)findViewById(R.id.password);

这行不通。您正在从字段初始值设定项调用 findViewById()。一般来说,您无法可靠地调用从 Activity 继承的方法。而且,对于 findViewById(),只有在调用 setContentView() 后才能获取结果。

将这些行替换为:

EditText userName;
EditText password;

然后,将 onCreate() 替换为:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText)findViewById(R.id.userName);
password = (EditText)findViewById(R.id.password);
}

关于java - 我的应用程序在 android studio 中崩溃了,在查看日志时我无法弄清楚出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302587/

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