【问题标题】:How to load url in android webview intercepted from from intent?如何从意图中加载 android webview 拦截表单中的 url?
【发布时间】:2017-11-05 03:46:55
【问题描述】:

我对android和java完全陌生,如果问一些琐碎的事情,请原谅。我有一个 webview 应用程序,只要有人从谷歌搜索或电子邮件中单击我的网站 url,它就可以正常打开。我在 AndroidManifest.xml 中加入了以下代码 [代码 1]。这工作得很好。每当我在 android 上单击我网站的 url 时,应用程序就会打开。

问题是我无法从意图中获取 url 并将其加载到 webview 应用程序中。例如,当用户单击链接 www.mywebsite.com/contact 时,它会打开应用程序并在 webview 元素中启动默认的 www.mywebsite.com。发生这种情况是因为我还没有传递从意图接收到的数据(url)并将该 url 加载到 webview 中。

我有代码来接收意图并加载 webview [代码 2],但它不工作。每次我使用意图打开应用程序时(通过单击谷歌搜索中的链接),该应用程序都会崩溃。我不知道将该代码放在 MainActivity.java 还是其他文件中。这是一个小代码,请让我知道我做错了什么。

[代码 1] ----------------------

<activity
   android:name=".activity.MainActivity"
   android:label="@string/app_name"
   android:launchMode="standard"
   android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
   <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>

   <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="http"
         android:host="www.mywebsite.com"
         android:pathPrefix="/"/>
   </intent-filter>

</activity>

[代码1] ------------

[代码2]------------

if(getIntent().getData()!=null)
{
Uri data = getIntent().getData();
String scheme = data.getScheme();
String fullPath = data.getEncodedSchemeSpecificPart();

combine = scheme+"://"+fullPath;
}

String url = null;
if(combine!=null)
{
url = combine;
webview.loadUrl(url);
}

[代码2]------------

将 combine 声明为 'String combine = null;'在文件的开头。

我从 StackOverflow 本身得到了这个 [代码 2],但我不知道我应该把它放在哪里,放在哪个文件中,在什么函数下。

这是来自 Play 控制台崩溃部分的错误日志。

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2659)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)
  at android.app.ActivityThread.-wrap12 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1473)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6123)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)

Caused by: java.lang.NullPointerException: 
  at com.mytemplates.webviewapp.activity.MainActivity.onCreate (MainActivity.java:94)
  at android.app.Activity.performCreate (Activity.java:6672)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2612)

【问题讨论】:

  • 当应用崩溃时显示你的 logcat
  • 问题是我不知道应该把 getIntent [代码 2] 放在哪里。我尝试将它放在 MainActivity.java 中的 @Override public void onStart() 或 public void onCreate(Bundle savedInstanceState) 下,但没有工作。你能帮我理解一下代码应该放在哪里吗?
  • 那会进入 onCreate,发布你的崩溃日志让我们更好地理解
  • 请查看 Play Consol 的崩溃日志。
  • Dude - NullPointerException 带有明确的行参考 - 当您查看代码时 - MainActivity.java 的第 94 行到底有什么?添加行本身,顺便说一句,这是“什么是 NullPointerException”线程的重复,我还没有标记,但你需要阅读它,因为这是你真正的问题。

标签: android android-intent webview


【解决方案1】:

您应该将(代码 2)部分放在 MainActivity 的 onCreate 方法中,因为您在清单的这一部分中定义了句柄方案的意图过滤器,因此您应该在 MainActivity 类的 onCreate 方法中编写(代码 2)。

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

     Intent intent=getIntent();
     String action = intent.getAction();

   if (Intent.ACTION_VIEW.equals(action) && (intent.getData() != null))
   {
   Uri data = intent.getData();
   String scheme = data.getScheme();
   String fullPath = data.getEncodedSchemeSpecificPart();
   combine = scheme+"://"+fullPath;
   }

String url = null;
if(combine!=null)
{
 url = combine;
 webview.loadUrl(url);
}

您可以使用this link 文档了解更多信息

【讨论】:

  • 我尝试了代码并放在 onCreate 方法下。该应用程序仍在崩溃。 Play 管理中心的崩溃日志文件包含在问题中。
  • 我也试过这段代码,问题似乎出在 webview.loadUrl(url);线。如果我跳过这条线,应用程序不会失败,但也不会工作。打开默认主页。
猜你喜欢
  • 2012-11-25
  • 1970-01-01
  • 2011-05-26
  • 2021-12-18
  • 1970-01-01
  • 2015-10-28
  • 2018-08-08
  • 1970-01-01
  • 2011-07-25
相关资源
最近更新 更多