2015-02-11 28 views
0

我在我的浏览器应用程序中实现了“< category android:name =”android.intent.category.BROWSABLE“/>” 我想要任何其他浏览器显示列表以选择要处理的应用程序该链接,其工作正常,我能够从列表中启动我的应用程序,但是它不会在我的应用程序中加载该网址,它所做的只是打开我的应用程序并将该网址粘贴到文本字段中。任何线索?从可浏览的意图加载网址android

编辑:我试图将其粘贴,但应用程序生效后在我的WebView这一个装载URL关闭

TextView uri = (TextView) findViewById(R.id.urlField); 
    //if (Intent.ACTION_MAIN.equals(getIntent().getAction())) { 
     String intentUri = (new Intent("com.example.browsableintent.MY_ACTION")) 
       .toUri(Intent.URI_INTENT_SCHEME).toString(); 
     uri.setText(intentUri); 
     webView.loadUrl(intentUri); 
     Log.w("URLHandler", intentUri); 
    //} else { 
     Uri data = getIntent().getData(); 
     if (data == null) { 
      uri.setText(""); 
     } else { 
      uri.setText(getIntent().getData().toString()); 
     } 
    //} 

清单

  <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <category android:name="android.intent.category.APP_BROWSER" /> 
     </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" /> 
      <data android:scheme="https" /> 
      <data android:scheme="about" /> 
      <data android:scheme="javascript" /> 
     </intent-filter> 

     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 

      <category android:name="android.intent.category.BROWSABLE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 

      <data android:scheme="http" /> 
      <data android:scheme="https" /> 
      <data android:scheme="inline" /> 
      <data android:mimeType="text/html" /> 
      <data android:mimeType="text/plain" /> 
      <data android:mimeType="application/xhtml+xml" /> 
      <data android:mimeType="application/vnd.wap.xhtml+xml" /> 
     </intent-filter> 
     <!-- For viewing saved web archives. --> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 

      <category android:name="android.intent.category.BROWSABLE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 

      <data android:scheme="http" /> 
      <data android:scheme="https" /> 
      <data android:scheme="file" /> 
      <data android:mimeType="application/x-webarchive-xml" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.WEB_SEARCH" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 

      <data android:scheme="" /> 
      <data android:scheme="http" /> 
      <data android:scheme="https" /> 
     </intent-filter> 

LOG

02-11 17:50:48.678: E/AndroidRuntime(29023): FATAL EXCEPTION: main 
02-11 17:50:48.678: E/AndroidRuntime(29023): Process: com.air.swiftmp, PID: 29023 
02-11 17:50:48.678: E/AndroidRuntime(29023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.air.swiftmp/com.air.swiftmp.MainActivity}: java.lang.NullPointerException 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.ActivityThread.access$800(ActivityThread.java:163) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.os.Handler.dispatchMessage(Handler.java:102) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.os.Looper.loop(Looper.java:157) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.ActivityThread.main(ActivityThread.java:5335) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at java.lang.reflect.Method.invokeNative(Native Method) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at java.lang.reflect.Method.invoke(Method.java:515) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at dalvik.system.NativeStart.main(Native Method) 
02-11 17:50:48.678: E/AndroidRuntime(29023): Caused by: java.lang.NullPointerException 
02-11 17:50:48.678: E/AndroidRuntime(29023): at com.air.swiftmp.MainActivity.onCreate(MainActivity.java:95) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.Activity.performCreate(Activity.java:5389) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
02-11 17:50:48.678: E/AndroidRuntime(29023): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256) 

回答

1

我会直接切入正题,如果

all it does is open my app and paste the url in the text field.

这将意味着,

if (Intent.ACTION_MAIN.equals(getIntent().getAction())) { 

返回true

我会假设你浏览的意图也是应用程序的主要意图。

所以你'如果'做的是检查你的'意图'(这也是你的可浏览)是'主要意图',这是真的。

编辑:

好了,基本上你检查,如果你的正在打开浏览的屏幕是当它是双头呆您的应用程序应该显示在屏幕上。

在你的Android清单你必须像

<intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
</intent-filter> 

这是您浏览的意图。当您调用getIntent()。getAction()时,它将返回与Intent.MAIN_ACTION相同的值。那是因为你在清单中定义了它。这没什么错。

<action android:name="android.intent.action.MAIN" /> 

其次,您希望加载应用在屏幕打开时获取的URL。 我对此没有任何经验,但我强烈建议查看this链接,因为它似乎解释了使用webViews加载应用程序中的URL。

我希望这能为您提供足够的答案并解决一些困惑!

EDIT2:

我要猜你havnt,无论是初始的网页视图,或havnt设定的内容查看关于web视图

所以尝试更换您的

setContentView(<Arbitary Thing refering to most likely a layout>); 

WebView webview = new WebView(this); 
setContentView(webview); 

由于哟日子会把你可以指的另一个布局,则无法加载XML你的TextView,所以我建议删除

TextView uri = (TextView) findViewById(R.id.urlField); 
uri.setText(intentUri); 

和其他地方,你可能指的URI。

我还建议阅读this链接中提供的示例,因为它看起来是一个很好的开始。除此之外,只要尝试和谷歌的WebView教程。

然后看着你的日志,在95行。你的MainActivity的东西返回null。如果我的建议不起作用,你能告诉我什么是95线吗?

EDIT3:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     WebView webview = new WebView(this); 
     setContentView(webview); 

     webView.loadUrl("http://www.google.com"); 

    } 
} 

而且它添加到您的Android清单只是标签下方

<uses-permission android:name="android.permission.INTERNET" /> 

/**************** ************************************************** ****/

既然你是新来的android让我解释最有可能导致你的错误:

当你

setContentView(R.id.layout_name); 

R.id提出将查找layout_name.xml这是由你的setContentView设置的任何引用。所以,如果你有layout_name_2.xml与假设R.id.urlField_2并尝试做

TextView uri = (TextView) findViewById(R.id.urlField2); 

它将找不到R.id.urlField2,因为它不是在你的LAYOUT_NAME定义。该活动只会在setContentView()中指定的布局中查找。有多种方法可以合并布局,因此您可以在1个活动中使用多个xml,但这是另一天的主题。

+0

尝试编辑您的问题,并显示您当前的代码和崩溃的错误日志。如果我不知道我在找什么,这很难帮助你! – 2015-02-11 12:03:11

+0

第95行是“Log.w(”URLHandler“,intentUri);”我有webviews的经验,我的webview很好 – 2015-02-11 12:54:10

+0

我想要的是每当用户在谷歌上查找视频时,因为您知道他/她可能会在搜索结果中看到YouTube视频,所以当他们点击该链接时,它应该询问他们想要什么链接打开(**这是工作正常**),我也能够启动我的应用程序,并让它粘贴到我的应用程序的地址栏中的网址。**但是**我希望它在将它粘贴到那里后立即加载该URL。 – 2015-02-11 13:12:21