该应用程序有三个活动,第二个包含6个带View Viewr的Web视图片段,我不想在按下后退按钮时返回上一个活动,但只需返回一个页面。我必须调用相关活动中的片段我知道,但我无法弄清楚代码无法正常工作的原因,当我转到应用程序崩溃的第二个活动时,这里是代码。任何专家?感谢
Web视图片段代码。
package com.example.AzaaadCompany.mymessenger;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment1 extends Fragment {
public Fragment1() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
final WebView web = (WebView) rootView.findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setLayerType(View.LAYER_TYPE_HARDWARE, null);
web.getSettings().setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);
web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
web.getSettings().setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
web.setInitialScale(228);
web.loadUrl("http://allrecipes.com/");
web.setWebChromeClient(new WebChromeClient());
web.setWebViewClient(new WebViewClient1());
return rootView;
}
private class WebViewClient1 extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
活动代码
package com.example.AzaaadCompany.mymessenger;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import java.util.ArrayList;
import java.util.List;
public class secondActivity extends AppCompatActivity implements
ViewPager.OnPageChangeListener,TabHost.OnTabChangeListener {
ViewPager viewPager;
TabHost tabHost;
android.support.v4.app.FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ReplaceFont.replaceDefaultFont(this, "DEFAULT", "birds.ttf");
manager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft= manager.beginTransaction();
Fragment1 f1= new Fragment1();
ft.add(R.id.webView,f1);
ft.addToBackStack(null);
ft.commit();
initviewpager();
initTabHost();
}
private void initTabHost() {
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
String[] tabNames = {"allrecipes", "zoomit", "p30download", "microsoft", "walmart", "amazon"};
for (int i = 0; i < tabNames.length; i++)
{
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec(tabNames[i]);
tabSpec.setIndicator(tabNames[i]);
tabSpec.setContent(new FakeContent(getApplicationContext()));
tabHost.addTab(tabSpec);
}
tabHost.setOnTabChangedListener(this);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int selectedItem) {
tabHost.setCurrentTab(selectedItem);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabChanged(String tabId) {
int selectedItem = tabHost.getCurrentTab();
viewPager.setCurrentItem(selectedItem);
HorizontalScrollView horizontalScrollView = (HorizontalScrollView) findViewById(R.id.myscrollview);
View tabView = tabHost.getCurrentTabView();
int scrollpos = tabView.getLeft() - (horizontalScrollView.getWidth() - tabView.getWidth()) / 2;
horizontalScrollView.smoothScrollTo(scrollpos, 0);
}
public class FakeContent implements TabHost.TabContentFactory {
Context context;
public FakeContent(Context mcontext) {
context = mcontext;
}
@Override
public View createTabContent(String tag) {
View fakeView = new View(context);
fakeView.setMinimumHeight(0);
fakeView.setMinimumWidth(0);
return fakeView;
}
}
private void initviewpager() {
viewPager = (ViewPager) findViewById(R.id.view_pager);
List<Fragment> listFragments = new ArrayList<Fragment>();
listFragments.add(new Fragment1());
listFragments.add(new Fragment2());
listFragments.add(new Fragment3());
listFragments.add(new Fragment4());
listFragments.add(new Fragment5());
listFragments.add(new Fragment6());
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), listFragments);
viewPager.setAdapter(myFragmentPagerAdapter);
viewPager.setOnPageChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}