所以我试图在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>
感谢所有帮助我的人,我完全解决了问题!
答案 0 :(得分:5)
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);
}
答案 1 :(得分:1)
将这些行移到Responsive
方法
onCreate
Somenthing like:
EditText userName = (EditText)findViewById(R.id.userName);
EditText password = (EditText)findViewById(R.id.password);
您是从字段初始值设定项中调用 public class MainActivity extends AppCompatActivity {'
EditText userName;
EditText password
@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);
}
而无法执行此操作。
答案 2 :(得分:0)
在EditText userName;
EditText password;
@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);
}
方法之后,在onCreate中移动findViewById方法调用。
includeArrayIndex