【问题标题】:How to open a url in webview on new screen based on button click如何根据按钮单击在新屏幕上的 webview 中打开 url
【发布时间】:2015-04-20 15:28:29
【问题描述】:

您好,我在 stackoverflow 中尝试了所有可用的解决方案,但没有任何效果,单击按钮时应用程序停止。请帮帮我。

每当我单击按钮时,应用程序就会崩溃。我希望应用程序在单击按钮时在 web 视图中打开 url。请帮帮我,非常感谢任何帮助。

任何人都可以发布修改后的代码。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >



<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:text="@string/button01" 
        android:onClick="onClick"/>
<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:text="@string/button02" 
        android:onClick="onClick"/>


</LinearLayout>   
------------------------------
screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >

<WebView 
    android:id="@+id/webview" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>
-----------------------------------------------------
mainactivity.java

package com.example.webview;



import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends ActionBarActivity {


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


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View v)
    {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        final Context context = this;
        Intent intent = new Intent(context, SecondActivity.class);
        startActivity(intent);
       switch(v.getId())
       {
           case R.id.button1:
               myWebView.loadUrl("http://techcrunch.com/tag/rss/");
                                break;

          case R.id.button2: 
              myWebView.loadUrl("http://www.bestchance.org.au/");
                               break;

          default:
                               break;
      }


}
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
--------------------
secondactivity.java
package com.example.webview;

import android.webkit.WebView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SecondActivity extends ActionBarActivity {

    private WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen3);




 }


}
-------------------------------

【问题讨论】:

    标签: java android button android-webview


    【解决方案1】:

    它崩溃的原因是因为您正在调用属于另一个活动的webview。因此,只需将一个字符串从您的活动传递到第二个活动,然后获取该字符串并将其加载到您的webview。以下是实现它的方法:

    在第一个活动中,将您的 onClick 方法编辑为:

    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
    
        switch(v.getId())
        {
            case R.id.action_bar:
                intent.putExtra("url", "http://techcrunch.com/tag/rss/");
                startActivity(intent);
                break;
    
            case R.id.action_bar_spinner:
                intent.putExtra("url", "http://www.bestchance.org.au/");
                startActivity(intent);
                break;
    
            default:
                break;
        }
    }
    

    然后在您的 SecondActivity 中:

    public class SecondActivity extends ActionBarActivity {
        private WebView myWebView;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen3);
    
        String url = getIntent().getStringExtra("url");
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl(url);
     }
    }
    

    【讨论】:

      【解决方案2】:

      您可能需要在此处发布代码以查看发生了什么,但如果您只是想获得一个 webview 弹出窗口,您可以启动一个浏览器活动来加载该页面

      String url = "http://www.example.com";
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(url));
      startActivity(i);
      

      如果你需要它作为你的活动,你需要将一个意图传递给你的新活动并获取 url,然后当你加载它时

      webView.loadUrl(URL);
      

      如果问题是崩溃,请在此处发布崩溃日志,以便我们看看出了什么问题

      【讨论】:

        【解决方案3】:
        public void onClick(View v)
            {
            WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.setWebViewClient(new WebViewClient());
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            final Context context = this;
            Intent intent = new Intent(context, SecondActivity.class);
            String url;
        
           switch(v.getId())
           {
               case R.id.button1:
                   url ="http://techcrunch.com/tag/rss/";
                                    break;
        
              case R.id.button2: 
                  url = "http://www.bestchance.org.au/";
                                   break;
        
              default:
                                   break;
          }
        startActivity(intent);
        myWebView.loadUrl(url);
        }
        

        只需删除您的代码并将其粘贴到..您试图访问不存在的按钮,因为您开始了一个新的活动,其中布局发生了变化并且没有按钮可以调用它们..

        【讨论】:

        • 如果这不起作用,请告诉我,我有另一个解决方案!
        • 嗨@Bsathvik 解决方案不起作用。应用再次崩溃。
        【解决方案4】:
        add Internet permission in your Manifest
        
        <uses-permission android:name="android.permission.INTERNET"/>
        
        activity_main.xml
        
        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:orientation="vertical">
        
            <WebView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/webview">
            </WebView>
            <androidx.appcompat.widget.AppCompatButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start"
                android:id="@+id/btn_start"
                android:textSize="20sp"
                android:background="@android:color/darker_gray"
                android:layout_marginTop="250dp"
                android:layout_gravity="center"
                android:textColor="@color/white"
                android:padding="30dp">
            </androidx.appcompat.widget.AppCompatButton>
        
        </LinearLayout>
        
        
        
        MainActivity.kt
        
        package com.example.webview
        
        import androidx.appcompat.app.AppCompatActivity
        import android.os.Bundle
        import android.webkit.WebViewClient
        import kotlinx.android.synthetic.main.activity_main.*
        
        class MainActivity : AppCompatActivity() {
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_main)
                btn_start.setOnClickListener { web_view() }
        
            }
        

        私人乐趣 web_view(){ webview.webViewClient = WebViewClient()

            // this will load the url of the website
            webview.loadUrl("your url")
        
            // this will enable the javascript settings
            webview.settings.javaScriptEnabled = true
        
            // if you want to enable zoom feature
            webview.settings.setSupportZoom(true) 
        }
        
            // if you press Back button this code will work
            override fun onBackPressed() {
                // if your webview can go back it will go back
                if (webview.canGoBack())
                    webview.goBack()
                // if your webview cannot go back
                // it will exit the application
                else
                    super.onBackPressed()
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多