使用action_view意图后Android应用程序崩溃

时间:2014-04-01 18:03:30

标签: java android eclipse

我有一个奇怪的错误,这是我的代码

String a;
String b;
a="http://website1.com/abcabc1";
String[] separated = a.split("/");
b = separated[3];

String url = "www.website2.com/" + b;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
finish();

我想要做的是获得.com /"之后的值。在字符串a"这是abcabc1"然后打开一个新网址,这将是" www.website2.com/abcabc1"但我遇到的问题是应用程序在启动时崩溃。我试图发现问题的根源,我发现如果我的代码是:

String url = "www.website2.com/";

它会毫无问题地工作,但如果是这样的话:

String url = "www.website2.com/" + b;

应用程序将崩溃。

注意如果我尝试将字符串url设置为textview文本,则会以正确的形式显示它,但如果我将其传递给浏览器则会崩溃。对不起,我的语言很差,并提前致谢。

更新: Catlog:

04-01 14:09:08.670: D/AndroidRuntime(2126): Shutting down VM
04-01 14:09:08.670: W/dalvikvm(2126): threadid=1: thread exiting with uncaught exception (group=0xb3ad7ba8)
04-01 14:09:08.680: E/AndroidRuntime(2126): FATAL EXCEPTION: main
04-01 14:09:08.680: E/AndroidRuntime(2126): Process: com.example.ytdownload, PID: 2126
04-01 14:09:08.680: E/AndroidRuntime(2126): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ytdownload/com.example.ytdownload.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.youtube-mp3.org/?e=session_expired&tx }
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.os.Looper.loop(Looper.java:136)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invoke(Method.java:515)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at dalvik.system.NativeStart.main(Native Method)
04-01 14:09:08.680: E/AndroidRuntime(2126): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.youtube-mp3.org/?e=session_expired&tx }
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Activity.startActivityForResult(Activity.java:3424)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Activity.startActivityForResult(Activity.java:3385)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Activity.startActivity(Activity.java:3627)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Activity.startActivity(Activity.java:3595)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at com.example.ytdownload.MainActivity.onCreate(MainActivity.java:36)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Activity.performCreate(Activity.java:5231)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-01 14:09:08.680: E/AndroidRuntime(2126):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-01 14:09:08.680: E/AndroidRuntime(2126):     ... 11 more

2 个答案:

答案 0 :(得分:5)

您必须在网址前设置"http://",这样才能正常工作!

String url = "http://www.website2.com/" + b;

答案 1 :(得分:1)

如果我可以提出建议,请回答同样的问题(花了我两小时才弄明白!!) - 我现在添加以下代码片段(特别是对于我要求用户提供网址的情况,但似乎即使是我自己提供它的情况也是一个好主意,因为我自己会忘记它:)

MoveInfo()

if (!(websiteString.contains("http://"))) { websiteString = "http://" + websiteString; } 当然是包含输入网址的字符串。 跳这有帮助:) P.S - 如果这是糟糕的编程习惯,我会很感激,如果有人评论它。我很擅长这个!