简体   繁体   English

单击按钮未打开URL

[英]URL not opening on button click

I'm pretty new on the Android development, and I'm starting my first application. 我在Android开发方面还很陌生,正在启动我的第一个应用程序。

I faced my first problem on the development, and the problem is "when clicking a button, the URL doesn't open, instead of that the app crashes." 我在开发中遇到了第一个问题,问题是“单击按钮时,URL不会打开,而导致应用程序崩溃。” .

This is MainActivity.java: 这是MainActivity.java:

package com.rodentsmobile.app;

import android.app.*;
import android.os.*;
import android.content.*;
import android.media.*;
import android.net.*;
import android.security.*;
import android.system.*;
import android.view.*;
import android.util.*;
import java.lang.*;
import java.net.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void ViewAptoide(View v)
    {
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
}

Firstly, it was public void ViewAptoide() but I thought that's forgetting the View v is the reason of the app crashing and not opening the URL.. But that wasn't the reason. 首先,它是public void ViewAptoide()但我认为忘记View v是应用程序崩溃的原因,而不是打开URL。.但这不是原因。

However, here is main.xml: 但是,这里是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <ImageView
        android:layout_height="300px"
        android:layout_width="700px"
        android:src="@drawable/image_1"
        android:layout_centerInParent="true"
        android:id="@+id/mainImageView1"/>

    <TextView
        android:layout_height="wrap_content"
        android:text="Rodents Mobile"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_below="@id/mainImageView1"
        android:layout_centerInParent="true"
        android:id="@+id/mainTextView1"/>

    <Button
        android:layout_height="wrap_content"
        android:text="Aptoide"
        android:layout_width="wrap_content"
        android:layout_below="@id/mainTextView1"
        android:layout_centerInParent="true"
        android:onClick="viewAptoide"
        android:translationY="15dp"/>

</RelativeLayout>

Thanks! 谢谢!

Your button calls the method 您的按钮调用方法

    android:onClick="viewAptoide"

While your method name is 当您的方法名称是

     public void ViewAptoide

Please ensure the naming is consistent otherwise your method is not found 请确保命名一致,否则找不到您的方法

In xml you have used 在xml中,您已使用

android:onClick="viewAptoide " .

And in activity you used 在活动中

 public void ViewAptoide (View v)
    {
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

Replace it with 替换为

 public void viewAptoide (View v)
    {
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

Check xml on click name and method name should be same. 检查xml的点击名称和方法名称应该相同。

The button's onClick attribute is set to 按钮的onClick属性设置为

android:onClick="viewAptoide"

Change your method name to 将您的方法名称更改为

public void viewAptoide

By convention, method names should start with a lowercase character in Java. 按照约定,在Java中,方法名称应以小写字母开头。

Also, since you are accessing the internet using ACTION_VIEW intent, make sure you have the following permission in your AndroidManifest.xml file 另外,由于您是使用ACTION_VIEW目的访问互联网的,因此请确保您在AndroidManifest.xml文件中具有以下权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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