Swipelayout java android error. When I put, gives application error!

Asked

Viewed 26 times

1

I’m wrong in this code in my application for Android, gives error:

package fabiohcnobre.hotelcolonialdosnobres;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {

    Button b1;
    EditText ed1;
    private WebView webView;
    private SwipeRefreshLayout swipeLayout;

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

        webView = new WebView(MainActivity.this);// webview in mainactivity
        setContentView(webView);// set the webview as the layout
        webView.getSettings().setJavaScriptEnabled(true);


        webView.setWebViewClient(new MyBrowser());

        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl("http://www.hotelcolonialdosnobres.com/mobile");
     swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
        @Override
        public void onRefresh() {
            webView.reload();


        }
    } );
    }

    private class MyBrowser extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

}
  • in his activity_main has a webview?

  • You need to show which error.

1 answer

1


You can’t use twice the setContentView();

The first time you are declaring your layout, but the second time when you call the setContentView(webView); you’re destroying the first.

You need to declare there in your xml activity_main, the webView, example:

<WebView
   android:id="@+id/webView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
  • Thanks !!! Gave right Leonardo Congratulations thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.