在“发射”片段后抽屉没有响应

时间:2016-11-27 20:36:23

标签: java android android-fragments webview

我一直在开发一个包含WebView的应用程序。我无法使WebView内容适合自己的设备屏幕(网站不支持手机)所以我使用 DisplayMetrics 来获取屏幕尺寸设备和规模的网站manualy与if子句取决于屏幕大小。

一切正常,直到我决定使用抽屉进行导航,因此使用碎片。

我的问题,打开应包含Webview的“FirstFragment”(LayoutEmulator显示Webview),此时不再使用抽屉了。抽屉出现但没有可能点击它打开其他碎片,打开“FirstFragment”后工具栏中的汉堡包图标也消失了。

MainActivity.java:

package user.app;

import android.app.FragmentManager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

{unnecessary code}

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        // Handle navigation view item clicks here.
        int id = item.getItemId();
        FragmentManager fragmentManager = getFragmentManager();

        if (id == R.id.nav_first_layout) {
           fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new FirstFragment())
                 .commit();
        } else if (id == R.id.nav_second_layout) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new SecondFragment())
                    .commit();
        } else if (id == R.id.nav_third_layout) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new ThirdFragment())
                    .commit();
        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

FirstFragment.java :(网址包含变量)

package user.app;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.util.Calendar;

    public class FirstFragment extends Fragment {


        View myView;

        @Nullable
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            myView = inflater.inflate(R.layout.first_layout, container, false);


            {from here to the end old code that worked properly in a public class}
            //get calendar week
            int kw = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) - 1;

            //get screen meassures
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            int dens = dm.densityDpi;
            double wi = (double) width / (double) dens;
            double hi = (double) height / (double) dens;
            double x = Math.pow(wi, 2);
            double y = Math.pow(hi, 2);
            double screenInches = Math.sqrt(x + y);

                {if-clause that sets scale depending on screen size,
             int scale}

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            WebView myWebView = (WebView) myView.findViewById(R.id.webview);
            myWebView.loadUrl("http://www.website" + kw + "/w/example.htm");
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            myWebView.getSettings().setLoadWithOverviewMode(true);
            myWebView.getSettings().setUseWideViewPort(false);

            //WebView set scale
            myWebView.setInitialScale(scale);

            return myView;

        }
    }

1 个答案:

答案 0 :(得分:1)

在您当前的示例中,尝试替换代码

WebView myWebView = (WebView) findViewById(R.id.webview);

WebView myWebView = (WebView) myView.findViewById(R.id.webview);