我不能在活动

时间:2016-09-24 11:41:23

标签: javascript java android android-fragments

我在方法中有TwoFragment



public class TwoFragment extends Fragment{
    Button btnweb;
    public WebView mWebView;

    public TwoFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v=inflater.inflate(R.layout.fragment_two, container, false);
        btnweb = (Button) v.findViewById(R.id.btnweb) ;
        btnweb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.btnweb:
                        mWebView.reload();
                        break;
                }
            }
        });


        mWebView = (WebView) v.findViewById(R.id.webview);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        //metodi optimizacii


        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        if (Build.VERSION.SDK_INT >= 19) {
            // chromium, enable hardware acceleration
            mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            // older android version, disable hardware acceleration
            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        }
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);


        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        return v;
    }

    public void reloadWebView() {
        mWebView.loadUrl("javascript:window.location.reload(true)");
    }




我在MainActivity尝试了调用方法,但崩溃了..

 public boolean onOptionsItemSelected(MenuItem item) {
       int id = item.getItemId();
        if (id == R.id.action_progress_show) {
            progress_menu_item.setActionView(R.layout.menu_item_layout);

//how i can call method here ??

 return true;

我的主要活动FUll



public class MainActivity extends AppCompatActivity {
    Menu mymenu;
    MenuItem progress_menu_item;
    private int[] tabIcons = {
            R.drawable.ic_home_white_24dp,
            R.drawable.ic_3d_rotation_white_24dp,
            R.drawable.ic_location_on_white_24dp

    };

    private Toolbar toolbar;
    private TabLayout tabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        CustomViewPager viewPager = (CustomViewPager ) findViewById(R.id.viewpager);
        viewPager.setPagingEnabled(false);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();




    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(), "ƏSAS");
        adapter.addFragment(new TwoFragment(), "VIRTUAL TUR");
        adapter.addFragment(new ThreeFragment(), "ÜNVANIMIZ");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        mymenu = menu;
        progress_menu_item = mymenu.findItem(R.id.action_progress_show);


        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_progress_show) {
            progress_menu_item.setActionView(R.layout.menu_item_layout);

//HERE NEEd TO CALL IN METHOd  fRAGMENT FOR REFRESH WEBVIEW ...

   return true;
        }

        return super.onOptionsItemSelected(item);
    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

试试这个:

为片段创建对象;

OneFragment frag1;
TwoFragment frag2;
ThreeFragment frag3;
CustomViewPager viewPager;

在onCreate()中:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        //------- CHANGE BELOW LINE--------

        viewPager = (CustomViewPager ) findViewById(R.id.viewpager);

        //-------
    }

在setupViewPager方法中:

private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        frag1=new OneFragment();
        frag2=new TwoFragment();
        frag3=new ThreeFragment();

        adapter.addFragment(frag1, "ƏSAS");
        adapter.addFragment(frag2, "VIRTUAL TUR");
        adapter.addFragment(frag3, "ÜNVANIMIZ");
        viewPager.setAdapter(adapter);
    }

在onOptionsItemSelected:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_progress_show) {
            progress_menu_item.setActionView(R.layout.menu_item_layout);

            if(viewPager.getCurrentItem()==1)
            {
                frag2.reloadWebView(); // Call method
            }

            return true;
        }

        return super.onOptionsItemSelected(item);
    }