我在oncreate和onOptionsItemSelected中有webview代码,它可以正常工作,如下所示。(这是我的旧代码,按要求工作)
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ mwebView = (WebView) findViewById(R.id.pdf_web_view);
WebSettings settings = mwebView.getSettings();
settings.setJavaScriptEnabled(true);
mwebView.setWebChromeClient(new WebChromeClient());
mwebView.loadUrl("file://" + getFilesDir() + "/index.html");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_gotopage:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View input_dialogView = layoutInflater.inflate(
R.layout.gotopage_dialogbox, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter the page number");
alert.setView(input_dialogView);
final EditText input = (EditText) input_dialogView
.findViewById(R.id.pagenum_edit);
input.requestFocus();
input.setTextColor(Color.BLACK);
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String pg_num = input.getText().toString();
if (pg_num.equals("")) {
input.setError("Page number Required");
} else {
mwebView.loadUrl("javascript:onGotoPage("
+ pg_num + ")");
}
}
});
alert.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
// create an alert dialog
AlertDialog alert1 = alert.create();
alert1.show();
return super.onOptionsItemSelected(item);
default:
return super.onOptionsItemSelected(item);
}
}
我想使用ViewPager将此代码转换为幻灯片页面。所以我有这样的代码(我的新代码):
MainActivity.java
public class Mainactivity extends AppCompatActivity {
private int NUM_PAGES=10;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pdf_webview);
mPager = (ViewPager) findViewById(R.id.pdf_view_pager);
mPagerAdapter = new
ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new SlideFragment().create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
SlideFragment.java
public class SlideFragment extends Fragment {
private static final String ARG_PAGE = "page";
private int mPageNumber;
private WebView mwebView;
public static SlideFragment create(int pageNumber) {
SlideFragment fragment = new SlideFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.pdf_fragment_webview, container, false);
mwebView = (WebView) findViewById(R.id.pdf_web_view);
WebSettings settings = mwebView.getSettings();
settings.setJavaScriptEnabled(true);
mwebView.setWebChromeClient(new WebChromeClient());
mwebView.loadUrl("file://" + getFilesDir() + "/index.html");
mwebView.loadUrl("javascript:onGotoPage("
+ pg_num + ")");
}
}
webview.loadURL仅适用于第一次,第二次无效。但是当它在onOptionsItemSelected方法中时,它工作正常。
我已尝试过WebView loadUrl works only once
的所有解决方案还试过onPageFinished并评估函数。
但没有一个在我的情况下有效。我无法弄清楚出了什么问题。
答案 0 :(得分:0)
我认为在html页面加载和运行的javascript函数之间存在竞争条件。
建议您先加载html页面,然后在onPageFinished回调中运行javascript。
此外,在webview上运行javascript代码时,您应该使用evaluateJavascript方法。
希望这有帮助。