java - 安卓 : webview not displayed on the defined activity

标签 java android webview

我想在同一个 Activity 上显示 WebView 和 ListView 。 如果我在 web View 中显示一个简短的 html 代码,那就可以了,一半的窗口专用于 web View 。 但是,如果我在 web View 中加载 url,则专用于 web View 的屏幕部分是空的,web View 的内容将显示在新 Activity 上。 你能帮我吗?

.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/wikipediaCompo"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <ListView
        android:id="@+id/list_oeuvres"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

java代码:

public class Display2 extends Activity {

    public ListView mListOeuvres;
    public WebView wikipediaCompo;

    public String[] infoOeuvres = new String[6];
    public int i=0;
    public String debug = "debug";

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

        mListOeuvres = (ListView) findViewById(R.id.list_oeuvres);
        try {
            InputStream is = getAssets().open("oeuvres.txt");
            Reader reader = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(reader);

            String line;
            while ((line = br.readLine()) != null) {
                //Log.e("code",line);
                String[] RowData = line.split("\\*");
                String nom = RowData[0];
                String refYouTube = RowData[1];
                String date = RowData[2];
                String duree = RowData[3];
                String interpretes = RowData[4];
                infoOeuvres[i] = nom +", "+date+" "+duree+" ("+interpretes+")";
                i++;
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        i=i-1;

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Display2.this,
                android.R.layout.simple_list_item_1, infoOeuvres);

        String url="https://en.wikipedia.org/wiki/Ferdinando_Bertoni";

        wikipediaCompo = (WebView) findViewById(R.id.wikipediaCompo);

        // Configure la webview pour l'utilisation du javascript
        WebSettings webSettings = wikipediaCompo.getSettings();
        //WebSettings.setJavaScriptEnabled(true);

        // Permet l'ouverture des fenêtres
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        // Autorise le stockage DOM (Document Object Model)
        webSettings.setDomStorageEnabled(true);

        // Load the url
        wikipediaCompo.loadUrl(url);
        // Test with html code
        /*String temp = "<html><body>" + "<p align=\"justify\">"
                + "TEST" + "</p> "
                + "</body></html>";
        wikipediaCompo.loadData(temp, "text/html", "utf-8");*/

        mListOeuvres.setAdapter(adapter);
        mListOeuvres.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String pos = String.valueOf(parent.getItemAtPosition(position));

                final GlobalClass globalVariable = (GlobalClass) getApplicationContext();

                //Set name and email in global/application context
                globalVariable.setNoOeuvre(position);

                Intent i = new Intent(Display2.this, Display3.class);
                startActivity(i);
            }
        });
    }
}

最佳答案

wikipediaCompo.loadUrl(url); 之后添加此代码

wikipediaCompo.setWebViewClient(new WebViewClient()
{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
           return false;
        }
 });

关于java - 安卓 : webview not displayed on the defined activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46051710/

相关文章:

android - 没有从 CustomAdapter 调用 getView

android - 使用 Android Espresso 框架模拟用户在系统 Activity 中的点击

ajax - PhoneGap/jQuery Mobile 应用程序是否允许 'POST' 跨域 ajax 请求?

java - 将列表类型添加到关联

java - ClientTransportException : HTTP transport error: java. lang.UnsupportedOperationException:方法未实现

安卓 : how to reload custom markers once the image is downloaded via Picasso?

android - 在 TabLayout 中使用多个 webview 来处理内存问题的逻辑

java - 如何在java中的类中声明对象的ArrayList?

java - 使用 docker exec 作为标签传递命令行参数

html - 在 iOS 7 或 8 的网页 View 中使用不同的样式表