2012-03-27 51 views
0

我有一個CustomWebViewClass:似乎無法通過PARAMS同構造函數

import android.app.Activity; 
import android.content.Intent; 

import android.os.Bundle; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class CustomWebView extends Activity{ 

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

     Intent intent = getIntent(); 
     String url = intent.getStringExtra("url"); 

     WebView webview = new WebView(this); 
     setContentView(webview); 
     webview.getSettings().setJavaScriptEnabled(true); 

     //progress bar optional 
     getWindow().requestFeature(Window.FEATURE_PROGRESS); 

     final Activity activity = this; 
     Toast.makeText(activity, "YO! " + url, Toast.LENGTH_SHORT).show(); 
     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
       // Activities and WebViews measure progress with different scales. 
       // The progress meter will automatically disappear when we reach 100% 
       activity.setProgress(progress * 1000); 
      } 
     }); 
     webview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     webview.loadUrl(url); 
    } 
} 

從另一個類(本質上是一個按鈕)我想調用這個類的意圖傳遞給它的URL像這樣:

Intent webView = new Intent(getContext(), CustomWebView.class); 
webView.putExtra("url", "http://google.com"); 
webView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getContext().startActivity(webView); 

但我得到一個黑屏或一個錯誤。顯然,我做錯了什麼,請幫助

哦,我的表現有這樣的:

<activity android:name=".CustomWebView" 
    android:label="CustomWebView" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
    android:screenOrientation="landscape"> 
    <intent-filter> 
     <action android:name="com.sapientnitro.lcinstore2.CUSTOMWEBVIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

回答

0

當您在Intent通過params,你可以用getIntent().getExtra[...]

檢索該試試這個:

import android.app.Activity; 

import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class CustomWebView extends Activity{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     String url = getIntent().getStringExtra("url"); 

     //super.onCreate(savedInstanceState); 


     WebView webview = new WebView(this); 
     setContentView(webview); 
     //progress bar optional 
     getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     // alllow js 
     webview.getSettings().setJavaScriptEnabled(true); 

     final Activity activity = this; 
     Toast.makeText(activity, "YO! " + url, Toast.LENGTH_SHORT).show(); 
     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
      // Activities and WebViews measure progress with different scales. 
      // The progress meter will automatically disappear when we reach 100% 
      activity.setProgress(progress * 1000); 
      } 
     }); 
     webview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     webview.loadUrl(url); 



    } 


} 
+0

感謝我試着但有些東西仍然是錯誤的,因爲我得到這個錯誤:03-27 10:50:30.240:E/Android運行時(6073):java.lang.RuntimeException:無法實例化活動ComponentInfo {com.sapientnitro.lcinstore2/com.sapientnitro.lcinstore2.CustomWebView}:java.lang.InstantiationException:com.sapientnitro.lcinstore2.CustomWebView – erik 2012-03-27 15:50:25

+0

似乎是最終的問題是:getWindow()。requestFeature(Window.FEATURE_PROGRESS);在內容之前被調用?我剛剛評論它,現在它的作品.. – erik 2012-03-27 16:22:14

0

你不帶參數調用它,你開始的活動,並把意圖捆綁包中的額外字符串,您永遠不會使用該捆綁包。

你需要像這樣在CustomWebView開頭:

Bundle extras = getIntent().getExtras(); 
String url = extras.getString("url"); 
0

你似乎誤解了活動的構造函數及其onCreate()方法之間的差異。

當您顯式創建一個新對象時,會調用構造函數;

Activity customWebView = new CustomWebView(url); 

這可能不是您想要創建新活動的方式。相反,你想使用意圖系統(這是你已經開始做)。

Intent webView = new Intent(getContext(), CustomWebView.class); 
webView.putExtra("url", "http://google.com"); 
webView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getContext().startActivity(webView); 

使用意圖,系統會調用一個空的構造,然後一旦該活動已創建,將調用onCreate()方法,你應該重寫。從這裏開始,您可以訪問您用來啓動Activity的Intent,並且可以獲取您作爲參數傳遞的URL。

你可能想要的東西,看起來像這樣:

public class CustomWebView extends Activity { 

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

     Intent intent = getIntent(); 
     String url = intent.getStringExtra("url"); 

     //Do stuff with your URL 
    } 
} 
+0

我更新了我的代碼以上反映您的答案,並得到此錯誤:03-27 10:59:17.830:E/AndroidRuntime(6870):java.lang。RuntimeException:無法啓動活動ComponentInfo {com.sapientnitro.lcinstore2/com.sapientnitro.lcinstore2.CustomWebView}:android.util.AndroidRuntimeException:requestFeature()必須在添加內容之前調用 03-27 10:59:17.830:E/AndroidRuntime(6870):導致:android.util.AndroidRuntimeException:在添加內容之前必須調用requestFeature() – erik 2012-03-27 16:00:42

+0

錯誤是在添加內容之前必須調用requestFeature()。在調用setContentView(webview)之前,必須調用getWindow()。requestFeature(Window.FEATURE_PROGRESS);' – theisenp 2012-03-27 17:04:46

1

requestFeature()應該setContentView之前調用:

...

this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
setContentView(webview); 

...