简体   繁体   中英

Android Studio OnClickListener doesn't work

I am trying to create a button that when it is pressed redirects the user on a web page!! I am using an OnClickListener for that!! However, when I press the button my application stops!! This is the error log I get:

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)

And this is my event code:

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);
            }
        });

Try something like that with android.intent.action.VIEW and not 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);
      }
    });

  }

}

Replace this:

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

with this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM