我知道有很多关于这个问题的问题,但似乎并没有真正回答。我正在从webservice加载一个html页面到webview,然后我想在html页面的列表中按下按钮时转到另一个活动。但是当我这样做时,webview变成了空白,我收到了这个错误: 无法调用determinVisibility() - 从未看到pid的连接:22985。 我该怎么办?
package com.myapp.tabbar;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private TextView text;
private WebView webView;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.month);
tabLayout = (TabLayout)findViewById(R.id.tablayout_id);
appBarLayout = (AppBarLayout)findViewById(R.id.appbarid);
webView = (WebView)findViewById(R.id.webview);
Calendar cal = Calendar.getInstance();
Date c = cal.getTime();
for(int i =0;i<7;i++)
{
String day = "" + android.text.format.DateFormat.format("EEEE", c);
String day1 = (String) DateFormat.format("dd", c);
String monthString = (String) DateFormat.format("MMM", c);
String year = (String) DateFormat.format("yyyy", c);
String MonthYear = monthString.toUpperCase()+","+year;
String day2 = day.substring(0, Math.min(day.length(), 3));
SpannableStringBuilder str1 = new SpannableStringBuilder(day2);
SpannableStringBuilder str = new SpannableStringBuilder(day1);
str1.setSpan(new ForegroundColorSpan(R.color.temp), 0, day2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str1.setSpan(new RelativeSizeSpan(0.4f), 0, day2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, day1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setText(MonthYear);
SpannableStringBuilder temp = new SpannableStringBuilder(str1+"\n"+str);
tabLayout.addTab(tabLayout.newTab().setText(temp));
cal.add(Calendar.DATE, 1);
c = cal.getTime();
}
Fetch_data fetch_data = new Fetch_data();
fetch_data.execute();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab)
{
Fetch_data fetch_data = new Fetch_data();
fetch_data.execute();
}
@Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// webView.setOnTouchListener(new View.OnTouchListener() {
// @Override
// public boolean onTouch(View view, MotionEvent motionEvent) {
// openActivity2("30");
// return false;
// }
// });
}
public void openActivity2(String url)
{
Intent intent = new Intent(this,Main2Activity.class);
intent.putExtra("Id",url);
startActivity(intent);
}
class Fetch_data extends AsyncTask<String, Void, String>
{
String htmlcontent = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.wtf("onPreExecute","onPreExecute");
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> nameValuePairs = new HashMap<String, String>();
int i = tabLayout.getSelectedTabPosition();
Calendar cal = Calendar.getInstance();
Date c = cal.getTime();
cal.add(Calendar.DATE, i);
c = cal.getTime();
String modifiedDate= new SimpleDateFormat("yyyy-MM-dd").format(c);
cal.add(Calendar.DATE, 1);
c = cal.getTime();
String modifiedDate1= new SimpleDateFormat("yyyy-MM-dd").format(c);
nameValuePairs.put("categoryId","1");
nameValuePairs.put("fromDate",modifiedDate);
nameValuePairs.put("toDate",modifiedDate1);
String req;
try {
req = ConnectionRequests.POST("http://www.idsdemo.me/champsservice/generalservices.asmx/GetSchedule", nameValuePairs);
JSONObject JO = new JSONObject(req);
htmlcontent = JO.getString("ScheduleHTML");
}
catch (Exception e)
{
Log.wtf("ERROR FOUND : get_all_data req"," e : "+e.getLocalizedMessage());
return null;
}
return htmlcontent;
}
@Override
public void onPostExecute(String result) {
super.onPostExecute(result);
webView.loadData(result, "text/html", null);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
Log.wtf("url","----");
webView.setWebViewClient(new WebViewClient() {
boolean timeout = true;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.wtf("url", url);
openActivity2(url);
return true;
}
@Override
public void onPageCommitVisible(WebView view, String url)
{
super.onPageCommitVisible(view, url);
}
@Override
public void onPageStarted(final WebView view, final String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url)
{
timeout = false;
super.onPageFinished(view, url);
}
});
}
}
}