android onclick执行顺序,错误“无法执行android:onClick的方法”

施知
2023-12-01

我想通过sql制作登录按钮。当我运行该应用时,我遇到了一个onclick错误:

Process: com.example.mosab.hostlent, PID: 31057

java.lang.IllegalStateException: Could not execute method for android:onClick

at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)

at android.view.View.performClick(View.java:5609)

at android.view.View$PerformClick.run(View.java:22263)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6077)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Caused by: java.lang.reflect.InvocationTargetException

at java.lang.reflect.Method.invoke(Native Method)

at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)

at android.view.View.performClick(View.java:5609)

at android.view.View$PerformClick.run(View.java:22263)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6077)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

at com.example.mosab.hostlent.login.loginon(login.java:92)

at java.lang.reflect.Method.invoke(Native Method)

at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)

at android.view.View.performClick(View.java:5609)

at android.view.View$PerformClick.run(View.java:22263)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6077)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)这是我的onclick代码:

public void loginon(View v) {

String Email = email.getText().toString().trim();

String Password = password.getText().toString().trim();

String type = "login";

worker worker = new worker(this);

worker.execute(type,Email,Password);

}这是我的xml文件:

android:id="@+id/login"

android:layout_width="205dp"

android:layout_height="50dp"

android:layout_gravity="center"

android:adjustViewBounds="true"

android:background="@color/hostlent"

android:baselineAlignBottom="false"

android:contextClickable="false"

android:cropToPadding="false"

android:keepScreenOn="false"

android:nestedScrollingEnabled="false"

android:onClick="loginon"

android:saveEnabled="false"

android:scaleType="fitCenter"

app:srcCompat="@drawable/login"

tools:layout_editor_absoluteX="86dp"

tools:layout_editor_absoluteY="332dp" />我的课被onclick代码调用:

public class worker extends AsyncTask {

Context context ;

AlertDialog alertDialog;

worker (Context context){

Context ctx;

}

@Override

protected String doInBackground(String... params) {

String type = params[0];

String login_url = "http://192.168.1.239:8080/login.php";

if (type.equals("login")){

try {

String email = params[1];

String password = params[2];

URL url = new URL(login_url);

HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();

httpsURLConnection.setRequestMethod("post");

httpsURLConnection.setDoOutput(true);

httpsURLConnection.setDoInput(true);

OutputStream outputStream = httpsURLConnection.getOutputStream();

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));

String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+URLEncoder.encode(password,"UTF-8");

bufferedWriter.write(post_data);

bufferedWriter.flush();

bufferedWriter.close();

outputStream.close();

InputStream inputStream = httpsURLConnection.getInputStream();

BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream,"iso-8859-1"));

String result="";

String line="";

while ((line=bufferedReader.readLine())!=null){

result += line;

}

bufferedReader.close();

inputStream.close();

httpsURLConnection.disconnect();

return result;

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

@Override

protected void onPreExecute() {

alertDialog=new AlertDialog.Builder(context).create();

alertDialog.setTitle("login status");

}

@Override

protected void onPostExecute(String result) {

alertDialog.setMessage(result);

alertDialog.show();

}

@Override

protected void onProgressUpdate(Void... values) {

super.onProgressUpdate(values);

}

}我将这些代码添加到清单文件中:

"你能帮我了解哪些是我的错误?

 类似资料: