我想通过sql创建登录按钮。当我运行应用程序时,我遇到了一个onclick错误:
Sub NewButton()
Dim wb As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet 1")
Set wb2 = Workbooks.Open("File Directory of second workbook")
Set ws2 = wb2.Worksheets("Sheet 1")
ws.Range("A:I") = ws2.Range("A:I").Value
End Sub
这是我的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)
这是我的xml文件:
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);
}
我的课程由onclick代码调用:
<ImageButton
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" />
我将在清单文件中添加此代码:
public class worker extends AsyncTask<String,Void,String> {
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);
}
}
你能帮我理解哪个是我的错误吗?
答案 0 :(得分:0)
错误出现在以下两行之一
中String Email = email.getText().toString().trim();
String Password = password.getText().toString().trim();
变量电子邮件或密码为空。您需要将这些变量设置为onCreate()方法中的相应edittext字段。
如果仍然无法解决问题,请使用完整代码将问题更新为您的活动和布局文件xml。