致命异常:main(Android标准)



当我在设备上运行它时,它会崩溃,我的logcat如下:

2020-12-19 17:03:28.204 24798-24798/com.example.emergencynotificationhealthcare E/AndroidRuntime: 
FATAL EXCEPTION: main
Process: com.example.emergencynotificationhealthcare, PID: 24798
java.lang.RuntimeException: Unable to start activity       
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2799)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1573)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6605)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:999)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:889)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.emergencynotificationhealthcare.Login.onCreate(Login.java:42)
at android.app.Activity.performCreate(Activity.java:6964)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)

这是我的activity_register.xml:

<ImageView
android:layout_width="101dp"
android:layout_height="69dp"
android:transitionName="logo_logo"
android:src="@drawable/logo_new"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:fontFamily="@font/bungee"
android:text="Welcome"
android:transitionName="logo_text"
android:textColor="#ff8e88"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register to start"
android:textSize="16sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
app:counterEnabled="true"
app:counterMaxLength="15"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:transitionName="user_tran"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phoneNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:passwordToggleEnabled="true"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:transitionName="password_tran"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Type"
android:layout_marginLeft="5dp"
android:textSize="17dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Guardian"/>
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#ff8e88"
android:text="Register"
android:textColor="#fff"
android:transitionName="button_tran"/>
<Button
android:id="@+id/register_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Already have an account? LOGIN"
android:background="#00000000"
android:transitionName="login_register_tran"/>

这是我的登录名java:

Button callRegister, login_btn;
ImageView image;
TextView logoText, sloganText;
TextInputLayout username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
callRegister = findViewById(R.id.register_screen);
image = findViewById(R.id.imageView);
logoText = findViewById(R.id.logo_name);
sloganText = findViewById(R.id.slogan_name);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login_btn = findViewById(R.id.login_btn);
callRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this,Register.class);
Pair[] pairs = new Pair[7];
pairs[0] = new Pair<View,String>(image,"logo_logo");
pairs[1] = new Pair<View,String>(logoText,"logo_text");
pairs[2] = new Pair<View,String>(sloganText,"logo_desc");
pairs[3] = new Pair<View,String>(username,"user_tran");
pairs[4] = new Pair<View,String>(password,"password_tran");
pairs[5] = new Pair<View,String>(login_btn,"button_tran");
pairs[6] = new Pair<View,String>(callRegister,"login_register_tran");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Login.this,pairs);
startActivity(intent, options.toBundle());
}
}
});
}

这是我的activity_login.xml

<ImageView
android:layout_width="101dp"
android:layout_height="69dp"
android:src="@drawable/logo_new"
android:transitionName="logo_logo"/>
<TextView
android:id="@+id/logo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/bungee"
android:text="Hi, Welcome Back!"
android:transitionName="logo_text"
android:textColor="#ff8e88"
android:textSize="40sp"/>
<TextView
android:id="@+id/slogan_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign in to continue"
android:transitionName="logo_desc"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/username"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="3dp"
android:background="#00000000"
android:elevation="0dp"
android:text="Forget Password" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ff8e88"
android:text="Log In"
android:textColor="#fff" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="#00000000"
android:elevation="0dp"
android:fontFamily="@font/antic"
android:text="New Member? REGISTER"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>

您是否膨胀了正确的布局文件?activity_register是您所说的文件的名称,但您的代码使用CCD_ 2。

如果不是这样的话,试着用setContentView(R.layout.activity_register);代替它。

最新更新