当我们运行应用程序时,这是我的android代码,当点击按钮时调用login方法时它会自动关闭
致命异常:主程序:soubhagya.hostinger,PID:25611 java.lang.IllegalStateException:无法执行方法 android:onClick at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 在android.view.View.performClick(View.java:5640)
在android.view.View $ PerformClick.run(View.java:22455)
在android.os.Handler.handleCallback(Handler.java:751)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
引起:java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
在 android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在android.view.View.performClick(View.java:5640)
在android.view.View $ PerformClick.run(View.java:22455)
在android.os.Handler.handleCallback(Handler.java:751)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.text.Editable android.widget.EditText.getText()'上 null对象引用
在soubhagya.hostinger.Login.logIn(Login.java:51)
在soubhagya.hostinger.Login.radhaJi(Login.java:106)
at java.lang.reflect.Method.invoke(Native Method)
在 android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在android.view.View.performClick(View.java:5640)
在android.view.View $ PerformClick.run(View.java:22455)
在android.os.Handler.handleCallback(Handler.java:751)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
这是登录活动。
public class Login extends AppCompatActivity {
//web url string
public static final String LOGIN_URL="http://abhinavin.000webhostapp.com/userregistration/login.php";
public static final String KEY_EMAIL="email";
public static final String KEY_PASSWORD="password";
public static final String LOGIN_SUCCESS="success";
public static final String SHARED_PREF_NAME="tech";
public static final String EMAIL_SHARED_PREF ="email";
public static final String LOGGEDIN_SHARED_PREF="loggedin";
private EditText editTextEmail;
private EditText editTextPassword;
private Button btn_SignIn;
private boolean loggedIn=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//this is text fields
editTextEmail=findViewById(R.id.email);
editTextPassword=findViewById(R.id.password);
btn_SignIn=findViewById(R.id.btn_signup);
}
//End of onCreate method
this login method is call on the click of button
private void logIn() {
//error is in this line i think.
//get value of email from edit text
final String email = editTextEmail.getText().toString();
//get value of password from edit text
final String password = editTextPassword.getText().toString();
StringRequest stringRequest=new StringRequest(Request.Method.POST, LOGIN_URL, new Response.Listener<String>() {
//override the onResponse method
@Override
public void onResponse(String response) {
//check condition
if (response.trim().equalsIgnoreCase(LOGIN_SUCCESS)) {
SharedPreferences sharedPreferences = Login.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putBoolean(EMAIL_SHARED_PREF, Boolean.parseBoolean(email));
editor.commit();
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
} else {
Toast.makeText(Login.this, "Invalid password", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
//overridre getParams
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> prams=new HashMap<>();
//put the data in map
prams.put(KEY_EMAIL, email);
prams.put(KEY_PASSWORD,password);
//return prams
return prams;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
End of login method
//overtide the onResume method
@Override
protected void onResume() {
super.onResume();
//get SharedPreferences
SharedPreferences sharedPreferences=getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
loggedIn=sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF ,false);
if(loggedIn)
{
//set Intent object
Intent i=new Intent(Login.this, MainActivity.class);
//StartActivity
startActivity(i);
}
}
//this is onclick function
public void btn_Click(View view) {
//call login function
logIn();
}
}
此代码编辑文本和按钮中的xml代码已定义
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="soubhagya.hostinger.Login">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText3" android:hint="Emile"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText4" android:hint="Password"/>
<Button
android:text="Signin"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/button3"
android:onClick="btn_Click"
/></LinearLayout>
这是我的xml代码,我们可以设计我们的应用程序
答案 0 :(得分:0)
btn_Click
来自android:onClick="radhaJi"
到android:onClick="btn_Click"
。
您的按钮初始化错误的ID
btn_SignIn=findViewById(R.id.btn_signup);
应该是
btn_SignIn=findViewById(R.id.button3);
答案 1 :(得分:0)
您需要像View.OnClickListener
一样实施Activity
,如下所示。
public class Login extends AppCompatActivity implements View.OnClickListener {
@Override
public void onClick(View view) {
if(view.getId() == R.id.btn_signup)
logIn();
}
}
并将布局更改为如下所示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Emile"
android:inputType="textPersonName" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Signin" />
</LinearLayout>