2016-12-15 64 views
-1

我想以編程方式將WebView添加到我的Activity。這是我的代碼:看不到WebView

public class MainActivity extends Activity { 

    private WebView wv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LinearLayout linearLayout = new LinearLayout(this); 
     linearLayout.setOrientation(LinearLayout.VERTICAL); 

     wv = new WebView(this); 
     wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 
     linearLayout.addView(wv); 

     super.setContentView(linearLayout); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     wv.loadUrl("www.google.com"); 
    } 
} 

這是AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.testing.kaarelp.learn"> 

    <application> 

     <uses-permission android:name="android.permission.INTERNET" /> 

     <activity android:name=".pakk.MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

但是當我打開應用程序,我得到一個空白屏幕。爲什麼不顯示www.google.com?

-------------編輯2 --------------

的問題不是在前面的推杆https://或(http://www.google.com。因此,它應該是:

wv.loadUrl("https://www.google.com"); 
+0

您不需要刪除問題。也許它會幫助別人。 –

回答

1

你的整個代碼是好的。您只需要使用http或https傳遞傳遞網絡鏈接。

webview wv; 
     @override 
     protected void oncreate(bundle savedinstancestate) { 
      super.oncreate(savedinstancestate); 

      linearlayout linearlayout = new linearlayout(this); 
      linearlayout.setorientation(linearlayout.vertical); 

      wv = new webview(this); 
      wv.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent)); 
      linearlayout.addview(wv); 

      super.setcontentview(linearlayout); 
     } 

    @override 
    protected void onresume() { 
     super.onresume(); 
     wv.loadurl("http://www.google.com"); 
    } 
1
protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      final WebView webView = (WebView) findViewById(R.id.fragment_about_us_wv); 

      webView.getSettings().setJavaScriptEnabled(true); 
      webView.loadUrl("https://www.google.co.in/"); 
      return view; 
     } 

和XML一樣

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <WebView 
     android:id="@+id/fragment_about_us_wv" 
     android:layout_width="match_parent" 
     android:background="@color/backGroundColor" 
     android:layout_height="match_parent" /> 

</LinearLayout>