我使用AsyncTask来验证服务器可用性。
class RetrieveFeedTask extends AsyncTask<String, String, Map<String, String>> {
private Exception exception;
ArrayList<String> str = new ArrayList<String>();
Map<String, String> statusMap = new HashMap<>();
private String job;
public ReturnData returnData;
public HomeFragment homeFragment = new HomeFragment();
public AsyncResponse delegate;
Socket socket = null;
public void params(String job){
this.job = job;
}
public void setAsyncResponse(AsyncResponse deletate){
this.delegate = deletate;
}
@Override
protected Map<String, String> doInBackground(String... params){
//ports: 8080 5060,5070,5080
if(job.equals("checkConn")) {
statusMap.clear();
boolean reachable = true;
boolean wwwServ = true;
try {
InetAddress address = InetAddress.getByName(params[0]);
reachable = address.isReachable(10000);
Log.d("lab", "IP: "+ reachable);
} catch (Exception e){
}
try {
(new Socket(params[0], 8080)).close();
}
catch (UnknownHostException e)
{
wwwServ = false;
}
catch (IOException e)
{
wwwServ = false;
}
Log.d("lab", "WWW: " + wwwServ);
Log.d("lab", "Rozmiar listy: " + Integer.toString(str.size()));
//add values to map
statusMap.put("IP", Boolean.toString(reachable));
statusMap.put("WWW", Boolean.toString(wwwServ));
}
return statusMap;
}
@Override
protected void onPostExecute(Map<String, String> result) {
// Result is here now, may be 6 different List type.
Log.d("lab", "The returned list contains " +result.size()+ "elements");
delegate.processFinish(result);
}
}
AsyncTask返回地图。一切都很好,直到我作为onCreateView的功能运行它。移动到onClick函数时出现问题。我这样做是因为,只有当用户点击了卡而不是按钮时,状态才会更新。
public class HomeFragment extends Fragment implements AsyncResponse{
Button checkConn;
TextView statusIP,statusWWW;
SharedPreferences savedSettings;
SharedPreferences.Editor editor;
private ArrayList<String> lista = new ArrayList<String>();
private Map<String, String> statusMap = new HashMap<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RetrieveFeedTask task = new RetrieveFeedTask();
task.setAsyncResponse(this);
task.params("checkConn");
savedSettings = getActivity().getSharedPreferences("lab.linuxservice.com.linuxservice", Context.MODE_PRIVATE); //load shared preferences
editor = savedSettings.edit();
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
statusIP = (TextView)rootView.findViewById(R.id.ipStatusText);
statusWWW = (TextView)rootView.findViewById(R.id.sipServerStatusText);
checkConn = (Button) rootView.findViewById(R.id.checkConn);
checkConn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("lab", "Rozmiar listy w onCreateView: " + Integer.toString(statusMap.size()));
String data[] = {savedSettings.getString("IP", "")};
task.execute(data); //chceck connection on click
String ipStatus = statusMap.get("IP");
String wwwStatus = statusMap.get("WWW");
Log.d("lab", "ipStatus: " + ipStatus);
if(ipStatus.equals("false")){
statusIP.setText("Unavailable");
}
else{
statusIP.setText("Available");
}
if(wwwStatus.equals("false")){
statusWWW.setText("Unavailable");
}
else{
statusWWW.setText("Available");
}
Toast.makeText(getActivity(), "Checking: ", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
public void processFinish(Map<String, String> output) {
this.statusMap = output;
}
}
如何在onClick中启动AsyncTask-得到此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at lab.linuxservice.com.linuxservice.HomeFragment$1.onClick(HomeFragment.java:132)
该行表示:
if(ipStatus.equals("false"))
当我等待9秒Thread.sleep(9000); Log.d("lab", "ipStatus wolam: " + ipStatus);
ipStatus
为空时,当我第二次单击按钮ipStatus
不为空时,为真。
更新
我这样做:
public TextView statusIP,statusWWW;
@Override
protected void onPostExecute(Map<String, String> result) {
// Result is here now, may be 6 different List type.
Log.d("lab", "The returned list contains " + result.size() + "elements");
delegate.processFinish(result);
homeFragment.updateView(view,result);
String ipStatus = statusMap.get("IP");
String wwwStatus = statusMap.get("WWW");
Log.d("lab", "ipStatus wolam: " + ipStatus);
statusIP = (TextView)view.findViewById(R.id.ipStatusText);
if(ipStatus.equals("false")){
statusIP.setText("Unavailable");
}
else{
statusIP.setText("Available");
}
if(wwwStatus.equals("false")){
statusWWW.setText("Unavailable");
}
else{
statusWWW.setText("Available");
}
Log.d("lab","wykonuje onPostExecute");
}
在RetrieveFeedTask
但是,收到错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
答案 0 :(得分:0)
您收到此错误,因为ipStatus
变量为null。 AsyncTask完成后设置此变量的值,并为您提供状态图。这意味着将行String ipStatus = statusMap.get("IP");
移动到AsyncTask的onPostExecute()
方法。