Android Studio OnClickListener不起作用

时间:2015-04-10 07:47:34

标签: android android-intent onclicklistener

我正在尝试创建一个按钮,按下该按钮会将用户重定向到网页上!!我正在使用OnClickListener !!但是,当我按下按钮时,我的应用程序停止了!!这是我得到的错误日志:

04-10 07:38:56.564      656-656/com.example.michalis.dei_college_application E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.actionVIEW dat=http://webmail.deicollege.gr/login.php }
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
            at android.app.Activity.startActivityForResult(Activity.java:3351)
            at android.app.Activity.startActivityForResult(Activity.java:3312)
            at android.app.Activity.startActivity(Activity.java:3522)
            at android.app.Activity.startActivity(Activity.java:3490)
            at com.example.michalis.dei_college_application.Login_home$1.onClick(Login_home.java:55)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

这是我的事件代码:

Button button = (Button)findViewById(R.id.buttonMail);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent viewIntent = new Intent ("android.intent.actionVIEW",Uri.parse("http://webmail.deicollege.gr/login.php"));
                startActivity(viewIntent);
            }
        });

2 个答案:

答案 0 :(得分:1)

尝试使用android.intent.action.VIEW,而不是android.intent.actionVIEW

 public class HelloWorld extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Assuming you are using xml layout
    Button button = (Button)findViewById(R.id.Button01);

    button.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        Intent viewIntent =
          new Intent("android.intent.action.VIEW",
            Uri.parse("http://www.stackoverflow.com/"));
          startActivity(viewIntent);
      }
    });

  }

}

答案 1 :(得分:1)

替换它:

Intent viewIntent = new Intent ("android.intent.actionVIEW",Uri.parse("http://webmail.deicollege.gr/login.php"));
            startActivity(viewIntent);

用这个:

String url = "http://webmail.deicollege.gr/login.php";
        Intent intent=new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);