Error SwipeLayout android java. When I put it, it gives an error in the application!

1

I'm having an error in this code in my Android application, it 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;
        }
    }

}
    
asked by anonymous 25.08.2016 / 22:02

1 answer

1

You can not use setContentView () twice,

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

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

<WebView
   android:id="@+id/webView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
    
25.08.2016 / 22:11