在我的Android应用中,我使用了两个单选按钮。
已选择其中之一并将其值存储在数据库中,但是rdbbut.getText()
在其Onclick
方法中抛出了空指针异常:
checkLogin(email, password, rdbbut.getText().toString())
我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
rggrp = (RadioGroup) findViewById(R.id.radioGroup);
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
final int selectedId = rggrp.getCheckedRadioButtonId();
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
rdbbut = (RadioButton) findViewById(selectedId);
// Check for empty data in the form
if (email.trim().length() > 0 && password.trim().length() > 0) {
// login user
checkLogin(email, password, rdbbut.getText().toString());
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
/**
* function to verify login details in mysql db
* */
private void checkLogin(final String email, final String password,
final CharSequence charSequence) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "login");
params.put("email", email);
params.put("password", password);
params.put("type", (String) charSequence);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
Logcat:
06-02 23:36:55.638: D/AndroidRuntime(1074): Shutting down VM
06-02 23:36:55.638: W/dalvikvm(1074): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-02 23:36:55.687: E/AndroidRuntime(1074): FATAL EXCEPTION: main
06-02 23:36:55.687: E/AndroidRuntime(1074): java.lang.NullPointerException
06-02 23:36:55.687: E/AndroidRuntime(1074): at com.kirti.login_screen.LoginActivity$1.onClick(LoginActivity.java:99)
06-02 23:36:55.687: E/AndroidRuntime(1074): at android.view.View.performClick(View.java:4084)
06-02 23:36:55.687: E/AndroidRuntime(1074): at android.view.View$PerformClick.run(View.java:16966)
06-02 23:36:55.687: E/AndroidRuntime(1074): at android.os.Handler.handleCallback(Handler.java:615)
06-02 23:36:55.687: E/AndroidRuntime(1074): at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 23:36:55.687: E/AndroidRuntime(1074): at android.os.Looper.loop(Looper.java:137)
06-02 23:36:55.687: E/AndroidRuntime(1074): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-02 23:36:55.687: E/AndroidRuntime(1074): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 23:36:55.687: E/AndroidRuntime(1074): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 23:36:55.687: E/AndroidRuntime(1074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-02 23:36:55.687: E/AndroidRuntime(1074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-02 23:36:55.687: E/AndroidRuntime(1074): at dalvik.system.NativeStart.main(Native Method)
06-02 23:36:58.688: I/Process(1074): Sending signal. PID: 1074 SIG: 9
我的Login.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_login"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/white"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/input_login"
android:textColorHint="@color/input_login_hint" />
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/white"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/input_login"
android:textColorHint="@color/input_login_hint" />
<!-- Login Button -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:weightSum="1"
android:id="@+id/radioGroup">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:padding="10dip"
android:text="@string/owner" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:padding="10dip"
android:text="@string/bikers" />
</RadioGroup>
<Button
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/btn_login_bg"
android:text="@string/btn_login"
android:textColor="@color/btn_login" />
<!-- Link to Login Screen -->
<Button
android:id="@+id/btnLinkToRegisterScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@null"
android:text="@string/btn_link_to_register"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
最佳答案
更改
rdbbut = (RadioButton) findViewById(selectedId);
至
rdbbut = (RadioButton) MainActivity.this.findViewById(selectedId);
关于android - 使用radiobutton.getText()时在logcat中获取空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30603719/