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

时间:2015-07-24 21:30:43

标签: android

我的活动就是这个,这是使用同名布局的LogInActivity,当我为按钮添加动作监听器时,应用程序崩溃 请帮助我,这是从最近两天发生的

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)

3 个答案:

答案 0 :(得分:3)

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();
}

原因是如果您不设置内容视图资源,则所有findViewByID方法都将返回null。

你也可以缩短它:

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

        }
    };
signUpButton.setOnClickListener(signupListener);

到此:

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

        }
    };
);

答案 1 :(得分:1)

在致电setIntentAndButton()之前,您正在呼叫setContentView() - 这是您与听众联系的地方。因此,当您尝试附加侦听器时,尚未创建按钮,并且对findViewById的调用将返回null。在尝试操作其中的对象之前,需要创建视图。

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

答案 2 :(得分:1)

试试这个:

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);
            setContentView(R.layout.activity_log_in);
            setIntentAndButton();
       }

        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) {
                    System.out.println("clicked");
                }
            };
            signUpButton.setOnClickListener(signupListener);
        }

    }