2017-02-15 56 views
0

我有兩個ImageButton的兩個不同的URL。當我點擊圖片時,它會返回一個白色的屏幕。我不知道是什麼問題。歡迎任何編輯或建議。我的網頁流量是空白

謝謝!

主要活動

public class main extends AppCompatActivity { 

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

     ImageButton amazon = (ImageButton)findViewById(R.id.imagebutton1); 
     ImageButton flipkart = (ImageButton)findViewById(R.id.imagebutton2); 

     amazon.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click 
       Intent intent = new Intent(main.this, MainActivity.class); 
       intent.setData(Uri.parse("http://www.amazon.com")); 
       WebView webview = new WebView(main.this); 
       setContentView(webview); 

       startActivity(intent); 



      } 
     }); 
     flipkart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getApplicationContext(),"Flipkart",Toast.LENGTH_LONG).show();// display the toast on you tube button click 
       Intent intent = new Intent(main.this, MainActivity.class); 
       intent.setData(Uri.parse("http://www.flipkart.in")); 
       startActivity(intent); 
      } 
     }); 

    } 

我的WebView活動

public class MainActivity extends AppCompatActivity { 

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

    WebView htmlWebView = (WebView) findViewById(R.id.webView); 
    htmlWebView.setWebViewClient(new CustomWebViewClient()); 
    WebSettings webSetting = htmlWebView.getSettings(); 
    webSetting.setJavaScriptEnabled(true); 
    webSetting.setDisplayZoomControls(true); 
    htmlWebView.loadUrl(""); 
} 



class CustomWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 
} 
+0

你永遠不會設置網址。您需要使用MainActivity中的getIntent()來獲取您傳遞類的數據的值。 – Mikerizzo

回答

0

你想加載地址在您的應用程序或者要通過外部瀏覽器打開。 anaway我給在網頁視圖中打開URL的剪斷代碼在你的應用程序如下爲:

WebView mynews; 
     ProgressBar pb; 
     @Override 
     protected void onCreate(@Nullable Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.newsreadscreen); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     pb= (ProgressBar) findViewById(R.id.progress_bar); 
      mynews= (WebView) findViewById(R.id.mynews); 
      mynews.getSettings().setJavaScriptEnabled(true); 
      mynews.getSettings().setDefaultFontSize(17); 
      mynews.getSettings().setDisplayZoomControls(true); 
      mynews.getSettings().setDomStorageEnabled(true); 
      mynews.getSettings().setLoadsImagesAutomatically(true); 



      mynews.setWebViewClient(new WebViewClient() { 

       @Override 
       public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       pb.setVisibility(View.VISIBLE); 
        view.loadUrl(url); 
        return true; 
       } 

       @Override 
       public void onPageFinished(WebView view, String url) { 
        pb.setVisibility(View.GONE); 
        super.onPageFinished(view, url); 
       } 

       @Override 
       public void onPageStarted(WebView view, String url, Bitmap favicon) { 
        pb.setVisibility(View.VISIBLE); 
        super.onPageStarted(view, url, favicon); 

       } 
      }); 

    mynews.loadUrl("https://www.google.co.in"); 

這是我編

amazon.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getApplicationContext(),"Amazon",Toast.LENGTH_LONG).show();// display the toast on home button click 
       Intent intent = new Intent(main.this, MainActivity.class); 
       intent.putExtra("url","http://www.amazon.com"); 
       startActivity(intent); 



      } 
     }); 

代碼得到在MainActivity值.Class onCreateMethod like below

String url=getIntent().getStringExtra("url"); 
mynews.loadUrl(url); 
+0

我想要在應用程序內打開網址。 –

+0

好比上面的代碼可以幫助你。 –

+0

我會盡快回複評論。 –