안드로이드 NullPointerException
조회수 625회
join_button = findViewById( R.id.join_button );
join_button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
final String UserNumber = join_number.getText().toString();
final String UserPwd = join_password.getText().toString();
final String UserName = join_name.getText().toString();
final String PassCk = join_pwck.getText().toString();
final String UserCollege = spinner_college.getSelectedItem().toString(); //선택된 값 가져오기.
//아이디 중복체크 했는지 확인
if (!validate) {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("학번이 중복인지 확인해주세요.").setNegativeButton("확인", null).create();
dialog.show();
return;
}
//한 칸이라도 입력 안했을 경우
if (UserNumber.equals("") || UserPwd.equals("") || UserName.equals("") || UserCollege.equals("소속대학을 선택하세요.")) {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("모두 입력해주세요.").setNegativeButton("확인", null).create();
dialog.show();
return;
}
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject( response );
boolean success = jsonObject.getBoolean( "success" );
//회원가입 성공시
if(UserPwd.equals(PassCk)) {
if (success) {
Toast.makeText(getApplicationContext(), String.format("%s님 가입을 환영합니다.", UserName), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
//회원가입 실패시
} else {
Toast.makeText(getApplicationContext(), "회원가입에 실패하였습니다.", Toast.LENGTH_SHORT).show();
return;
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("비밀번호가 동일하지 않습니다.").setNegativeButton("확인", null).create();
dialog.show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
//서버로 Volley를 이용해서 요청
RegisterRequest registerRequest = new RegisterRequest(UserNumber, UserPwd, UserName, UserCollege, responseListener);
RequestQueue queue = Volley.newRequestQueue( RegisterActivity.this );
queue.add( registerRequest );
}
});
<activity android:name=".Activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity.MainActivity"/>
<activity android:name=".Activity.RegisterActivity"/>
<activity android:name=".Activity.LoginActivity"/>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.LoginActivity">
<Button
android:id="@+id/login_button"
style="@style/defaultButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="로그인"
app:layout_constraintBaseline_toBaselineOf="@+id/join_button"
app:layout_constraintStart_toStartOf="@+id/login_password" />
<EditText
android:id="@+id/login_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="41dp"
android:ems="10"
android:hint="학번"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toTopOf="@+id/login_password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/login_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="41dp"
android:ems="10"
android:hint="비밀번호"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="@+id/login_button"
app:layout_constraintStart_toStartOf="@+id/login_number" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="41dp"
app:layout_constraintBottom_toTopOf="@+id/login_number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/join_button"
style="@style/defaultButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="190dp"
android:text="가입"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/login_password" />
</androidx.constraintlayout.widget.ConstraintLayout>
분명 정상적으로 돌아가던 코드가 갑자기 NullPointerException라고 합니다 ㅠㅠ
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.myapplication.Activity.RegisterActivity.onCreate(RegisterActivity.java:120)
setContentView( R.layout.activity_join);
이것도 작성했는데 도대체 무엇이 문제일까요 한번만 도와주세요 .. 안드로이드 초보자 입니다..
댓글 입력