2014-06-11 13 views
0

嘗試使用的WebView,我有以下代碼:Android應用程序與的WebView不工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.example.places.xt.MainActivity$PlaceholderFragment" > 

    <WebView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" /> 

</RelativeLayout> 

(...) 
import android.webkit.WebView; 
import android.os.Build; 

public class MainActivity extends ActionBarActivity { 

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

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

     WebView webview = (WebView) findViewById(R.id.webview); 
     setContentView(webview); 

     String summary = "<html><body style=\"background-color: yellow\">You scored <b>192</b> points.</body></html>"; 
     webview.loadData(summary, "text/html", null); 

    } 
(...) 

當我在「運行」點擊以部隊建設的APK文件,然後下載並在我的手機上啓動它,它說「應用程序已停止」。該應用程序的大小隻有約760 kB。我認爲它不是很多,我猜WebView包沒有正確導入。可能是什麼原因?

在測試了4.4奇巧,Eclipse的,Win 7的32位

+0

你的Logcat輸出是什麼? – Pr38y

+0

不知道如何讀取手機上的logcat輸出。 –

+0

@BarthZalewski從eclipse中讀取日誌cat文件 – wheatfairies

回答

1

首先,你的WebView是分段佈局,而不是在活動佈局。將訪問web視圖的代碼移動到片段的onCreateView()。請參閱NullPointerException accessing views in onCreate()

其次,刪除setContentView(webview)。從當前內容視圖查找視圖並將其設置爲內容視圖沒有實際意義。

相關問題