gpt4 book ai didi

android - 当我对按钮使用 OnClicklistener 时,我的应用程序崩溃了,即使它是空的

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:54 26 4
gpt4 key购买 nike

我的 Activity 是这个,这是使用同名布局的 LogInActivity,当我为按钮添加 Action 监听器时,应用程序崩溃请帮我解决这两天发生的问题

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

import java.io.Serializable;


public class LogInActivity extends ActionBarActivity {

private Button signUpButton;
private Button logInButton;
private Intent signUpChoiceIntent;
private OnClickListener signupListener;
private HumLogController humLogController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setIntentAndButton();
setContentView(R.layout.activity_log_in);
}

private void setIntentAndButton(){
signUpChoiceIntent = new Intent (this , SignUpChoiceActivity.class);
signUpButton = (Button) findViewById(R.id.firstSignupButton);
logInButton = (Button) findViewById(R.id.firstLoginButton);
signupListener = new OnClickListener() {
@Override
public void onClick(View v) {

}
};
signUpButton.setOnClickListener(signupListener);
}

}

布局文件,两个按钮正在创建所有问题:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".LogInActivity"
android:background="#ff0aff62"
android:id="@+id/logInRelativeLayout">


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/logInUsernameField"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="126dp"
android:textSize="30sp"
android:background="#ffffffff"
android:hint="@string/username_field_hint_text"
android:lines="1"
android:singleLine="true"
android:maxLength="40" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/logInPasswordField"
android:hint="@string/password_field_hint_text"
android:textSize="30sp"
android:background="#ffffffff"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="176dp"
android:lines="1"
android:singleLine="true"
android:maxLength="15" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sign_in_button_text"
android:id="@+id/firstLoginButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="226dp"
android:background="#fff54d70"
android:textSize="30sp"
android:textColor="#ffffffff" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sign_up_button_text"
android:id="@+id/firstSignupButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="376dp"
android:textSize="30sp"
android:background="#ff0aff62"
android:textColor="#ff000000"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/or_text"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_below="@+id/firstLoginButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="306dp"
android:layout_marginLeft="136dp"
android:layout_marginStart="136dp"/>

//I am not missing the /relative layout closing tag, it is just not here

这是堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.praduman.humlog/com.example.praduman.humlog.LogInActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.praduman.humlog.LogInActivity.setIntentAndButton(LogInActivity.java:41)
at com.example.praduman.humlog.LogInActivity.onCreate(LogInActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

最佳答案

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setIntentAndButton();
setContentView(R.layout.activity_log_in);
}

应该是:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
setIntentAndButton();
}

原因是如果不设置内容 View 资源,所有的findViewByID方法都会返回null。

你也可以缩短这个:

signupListener = new OnClickListener() {
@Override
public void onClick(View v) {

}
};
signUpButton.setOnClickListener(signupListener);

为此:

signUpButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

}
};
);

关于android - 当我对按钮使用 OnClicklistener 时,我的应用程序崩溃了,即使它是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31620109/

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