我已尽力而为,但无法解决此问题,我尝试调试没有任何反应,并尝试使新项目出现相同的错误。直到下午的项目都运行良好,但随后却出现了一个错误,只是试图更新Android应用程序的UI:
E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nodemcu.controlnodemcu, PID: 6681
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at com.example.nodemcu.nodemcucontroller.MainActivity$HttpRequestTask.<init>(MainActivity.java:80)
at com.example.nodemcu.nodemcucontroller.MainActivity$HttpRequestTask.<init>(MainActivity.java:68)
at com.example.nodemcu.nodemcucontroller.MainActivity.buttonClick(MainActivity.java:63)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.
导入的库:
package com.example.nodemcu.nodemcucontroller;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog.Builder;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URISyntaxException;
MainActivity.java的代码:
public class MainActivity extends AppCompatActivity {
final Context context = this;
private EditText ipAddress;
private Button ledOn, ledOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipAddress = (EditText) findViewById(R.id.edt_ip);
ledOn = (Button) findViewById(R.id.btn_ledOn);
ledOff = (Button) findViewById(R.id.btn_ledOff);
}
public void buttonClick(View view) {
String ledStatus;
if (ipAddress.getText().toString().equals(""))
Toast.makeText(MainActivity.this, "Please enter the ip address...", Toast.LENGTH_SHORT).show();
else {
if (view == ledOn)
ledStatus = "1";
else
ledStatus = "0";
String serverAdress = ipAddress.getText().toString() + ":" + "80";
HttpRequestTask requestTask = new HttpRequestTask(serverAdress);
requestTask.execute(ledStatus);
}
}
private static class HttpRequestTask extends AsyncTask<String, Void, String>{
private String serverAdress;
private String serverResponse = "";
AlertDialog dialog;
private WeakReference<MainActivity> activityReference;
HttpRequestTask(MainActivity context){
activityReference = new WeakReference<>(context);
}
private HttpRequestTask(String serverAdress) {
this.serverAdress = serverAdress;
dialog = new AlertDialog.Builder(activityReference.get())
.setTitle("HTTP Response from Ip Address:")
.setCancelable(true)
.create();
}
@Override
protected String doInBackground(String... params) {
dialog.setMessage("Data sent , waiting response from server...");
if (!dialog.isShowing())
dialog.show();
String val = params[0];
final String url = "http://" + serverAdress + "/led/" + val;
try {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet();
getRequest.setURI(new URI(url));
HttpResponse response = client.execute(getRequest);
InputStream inputStream = null;
inputStream = response.getEntity().getContent();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
serverResponse = bufferedReader.readLine();
inputStream.close();
} catch (URISyntaxException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (ClientProtocolException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
serverResponse = e.getMessage();
}
return serverResponse;
}
@Override
protected void onPostExecute(String s) {
dialog.setMessage(serverResponse);
if (!dialog.isShowing())
dialog.show();
}
@Override
protected void onPreExecute() {
dialog.setMessage("Sending data to server, please wait...");
if (!dialog.isShowing())
dialog.show();
}
}
}
Activity_main.xml文件:
<?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/edt_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/ip_address"
android:inputType="text"/>
<Button
android:id="@+id/btn_ledOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/led_off" />
<Button
android:id="@+id/btn_ledOn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/led_on" />
</LinearLayout>
答案 0 :(得分:0)
完成编辑
按照崩溃日志中的这一行
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference at com.example.nodemcu.nodemcucontroller.MainActivity$HttpRequestTask.
问题是您创建警报对话框时activityReference
为空。
您的two
中有HttpRequestTask
个构造函数。
编辑
像这样更改您的HttpRequestTask
构造函数:
private WeakReference<MainActivity> activityReference;
private HttpRequestTask(String serverAdress, MainActivity context) {
this.serverAdress = serverAdress;
this.activityReference = new WeakReference<>(context);
dialog = new AlertDialog.Builder(activityReference.get())
.setTitle("HTTP Response from Ip Address:")
.setCancelable(true)
.create();
}
答案 1 :(得分:-1)
尝试以下代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Context context = this;
private EditText ipAddress;
private Button ledOn, ledOff;
private String ledStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipAddress = (EditText) findViewById(R.id.edt_ip);
ledOn = (Button) findViewById(R.id.btn_ledOn);
ledOff = (Button) findViewById(R.id.btn_ledOff);
if (ipAddress.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, "Please enter the ip address...", Toast.LENGTH_SHORT).show();
} else {
ledOn.setOnclicklistener(this);
ledOff.setOnclicklistener(this);
}
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.ledOn:
ledStatus = "1";
break;
case R.id.ledOff:
if (ledStatus == "1") { ledStatus = "0"; }
break;
}
// do your async task here
}
}
按钮现在应该可以正常工作了。不知道您的异步任务,但是它也应该工作,这取决于它是从ledOn
或ledOff
按钮接收输入的,然后可以将代码放在此处,否则就可以了。尝试这种方式。