Listview onclick方法在没有URL链接时崩溃

时间:2016-03-19 16:19:09

标签: android listview url

你好我有一个listview填充数据当一些数据包含我希望能够点击并打开浏览器的URL链接。 我设法使用以下代码执行此操作:

          mylistView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) {

            if(Links[position]!= " ")
            {
              startActivity(new Intent(
             Intent.ACTION_VIEW,  Uri.parse(Links[position])));
            }
          }
      });

并添加:

   android:autoLink="web"
   android:linksClickable="true"

到我的xml。

我知道当我的链接数组没有URl时,它等于“”。 我的问题是没有URL的listview部分导致应用程序在单击时崩溃。所以URL工作正常,但是当没有点击url的listview的一部分时,我不希望发生任何事情。 有什么想法吗?

03-19 14:03:30.264: D/TextView(19847): onTouchEvent - action: 0, mEditor:      null, mMovement: null
03-19 14:03:30.414: D/AndroidRuntime(19847): Shutting down VM
03-19 14:03:30.414: W/dalvikvm(19847): threadid=1: thread exiting with     uncaught exception (group=0x41592898)
03-19 14:03:30.434: E/AndroidRuntime(19847): FATAL EXCEPTION: main
03-19 14:03:30.434: E/AndroidRuntime(19847): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=  }
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.Activity.startActivityForResult(Activity.java:3390)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.Activity.startActivityForResult(Activity.java:3351)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.Activity.startActivity(Activity.java:3587)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.Activity.startActivity(Activity.java:3555)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at com.example.corneforum.Threadactivity$1.onItemClick(Threadactivity.java:72)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.widget.AbsListView$1.run(AbsListView.java:3463)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.os.Handler.handleCallback(Handler.java:730)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.os.Looper.loop(Looper.java:152)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at android.app.ActivityThread.main(ActivityThread.java:5136)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at java.lang.reflect.Method.invoke(Method.java:525)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:744)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-19 14:03:30.434: E/AndroidRuntime(19847):    at dalvik.system.NativeStart.main(Native Method)

添加了崩溃日志

2 个答案:

答案 0 :(得分:0)

像这样使用Try / Catch:

try {
    statements you want to do; //such as clicking button
}
catch (RuntimeException e) {
    e.printStackTrace;
    Toast toast = Toast.makeText(this, "no URL found", Toast.LENGTH_SHORT);
    toast.show();
}

您可以找到所需的正确例外。示例:NumbeFormatException用于在输入字符串而不是数字时避免崩溃。

答案 1 :(得分:0)

好的家伙,我浪费了你的时间,总是进入if语句,这在日志中很明显。我道歉。 像

这样的东西
if(Links[position].contains("http"));

解决了我的问题。 谢谢你的帮助。