无法加载URL中的WebView(机器人)机器人、加载、URL、WebView

2023-09-12 05:33:04 作者:洅见洅吔囨见

我想在一个的WebView加载一个URL,但我不断收到一个错误。 (见下文)

I'm trying to load a URL in a webview but I keep getting an error. (see below)

这是在code,它在启动时加载的网址:

this is the code that should load the URL at startup:

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }


    WebView mywebview1;

       mywebview1 = (WebView)findViewById(R.id.myownwebview);
       mywebview1.loadUrl("http://www.mad-sharky.com");



}

我在XML文件中的WebView ID为myownwebview。每当我开始投影法得到的图片中显示的错误消息:

I have a webview in my XML file with id "myownwebview". Whenever i start the projecti get the error message shown in the picture:

我搜索了很多,最让我找到了答案告诉大家,网址写入视图创建之前,因此它抛出一个异常。它被认为是seContentView之后。但是,在我的情况下,它是继的setContentView 所以我找不到是什么原因造成的。这是我的第一个应用程序,所以裸跟我,如果这是一个基本的问题。

I searched a lot and most of the answers I found tell that the URL is written to the view before it is created and therefore it throws an exception. It is supposed to be after "seContentView". But in my case it is after setContentView so I can't find what is causing it. It is my first app so bare with me if it is a basic question.

感谢

更新

调试屏幕图像:

推荐答案

大概是片段的交易导致了异常,所以这个评论code:

probably the fragment transaction is causing the exception, so comment this code:

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

   /* if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }*/


    WebView mywebview1;

       mywebview1 = (WebView)findViewById(R.id.myownwebview);
       mywebview1.loadUrl("http://www.mad-sharky.com");


}

或加载的URL到从web视图的 PlaceholderFragment