Android 应用程序无法打开网页链接

标签 android xml android-intent browser

我在这个应用程序的末尾有一个按钮,可以将用户带到他们的默认浏览器和预定的链接。不幸的是,那个按钮坏了。我不确定这是 Java 还是 XML 的问题,但是附上了两者的代码。

package com.example.ldsm3;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity
import android.content.DialogInterface;
import android.content.Intent;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class Finished extends Activity {

    public void onClick(Button button1) 
    {

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL HERE"));
        startActivity(browserIntent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finished);

        // tv is the ID for the TextView in the XML file
        TextView tv = (TextView) findViewById(R.id.textView2); 

        // set the TextView to show the score
        tv.setText(Misc.correct + "/" + Misc.total);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.finished, menu);
        return true;
    }

}

还有 XML 代码...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Finished" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="69dp"
        android:text="@string/Finished" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginBottom="19dp"
        android:text="Go to the game!" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="93dp"
        android:text="0/0" />

</RelativeLayout>

我该如何解决这个问题?是因为XML还是Java?我没有正确定义 Intent 吗?

最佳答案

您需要将其添加到您的 onCreate() 方法中:

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
        //Do your stuff here
  }
});

关于Android 应用程序无法打开网页链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21816809/

相关文章:

android - 按下后退并重新打开时, fragment 不会在 viewpager 中恢复其状态

android - 当文本太长时,如何使 Google Play 像 TextView 动画一样?

java - 如何使用非主要 Activity 在统一游戏中捕获自定义网址?

平台类型的 Android Studio Kotlin 类型不匹配

java - RadioGroup 扩展 RelativeLayout?

android - Intent 过滤器甚至不会以编程方式注销

java - Android 应用程序在 startActivity() 上崩溃

c# - 如何仅将大型 xml 文件的一部分反序列化为 C# 类?

c# - XML 解析错误 - C#

android - 安卓开启全景时如何设置光球模式