2011-05-18 58 views
0

我有一个webview,我加载一个外部URL,所以这加载第一页,我点击链接去第二页,然后我点击第三页。保存WebView加载其他活动?

现在,在这第三页上我写了一个Javascript来打开另一个活动工作正常。 但是在这个活动结束时,我的WebView重新加载了第一个页面,而这个页面应该是相同的页面,在这个页面中的Activity被调用。

任何想法到底是什么,我做错了?

在此先感谢。

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import android.R.bool; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.CompressFormat; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Base64; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 

import android.widget.Toast; 

public class Cam2 extends Activity { 
    int CAMERA_PIC_REQUEST = 2; 

    WebView webview; 
    String imageSource; 
    boolean _hasImage = false; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     webview = (WebView) findViewById(R.id.webview); 
     webview.getSettings().setJavaScriptEnabled(true); 

     final Activity activity = this; 
     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
       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(); 
      } 

      @Override 
      public void onPageFinished(WebView view, String url) { 
       if (_hasImage) { 
        webview.loadUrl("javascript:AndroidSubmitToWeb();"); 
       } 
      } 
     }); 

     webview.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
     webview.loadUrl("http://192.168.0.5/SomeWeb"); 
    } 

    public void openCamera() { 
     Intent cameraIntent = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_PIC_REQUEST) { 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      String s = storeAndExit(thumbnail); 
      webview.loadUrl("javascript:function ImageData(){ return '" + s 
        + "';}"); 
      _hasImage = true; 
     } else { 
      Toast.makeText(Cam2.this, "Picture Not taken", 5000).show(); 
     } 
     super.onActivityResult(requestCode, resultCode, data); 
    } 

    public String storeAndExit(Bitmap data) { 
     ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream(); 
     Bitmap myMap = data; 
     try { 
      if (myMap.compress(CompressFormat.JPEG, 70, jpeg_data)) { 
       byte[] code = jpeg_data.toByteArray(); 
       byte[] output = org.apache.commons.codec.binary.Base64 
         .encodeBase64(code); 
       String js_out = new String(output); 
       return js_out; 
      } 
     } catch (Exception e) { 
      Toast.makeText(Cam2.this, 
        "STORE IMAGE EXCEPTION: " + e.getMessage(), 5000).show(); 
     } 
     finish(); 
     return null; 
    } 
} 
+0

请检查是否的onCreate()再次被调用,而你从你的第二个活动退出。 – 2011-05-18 08:19:59

+0

是的,它调用onCreate() – user581157 2011-05-18 08:30:23

回答

0

我想你在OnCreate应()mnethod

,网页视图已创建&显示网页。

只有你需要检查这就是所有。

最好的问候,

〜阿努普

+0

意思是我必须检查WebView是否创建如果创建,然后什么都不做加载URL的权利? – user581157 2011-05-18 09:36:32

+0

我试图让我的WebView null – user581157 2011-05-18 09:37:12

+0

你破坏你的第一个活动,同时创建另一个如果是的请不要这样做 – 2011-05-18 09:48:14