2014-02-16 90 views
2

我在這個應用程序的最後有一個按鈕,它將用戶帶到默認瀏覽器和預定的鏈接。不幸的是,那個按鈕壞了。我不確定這是Java還是XML的問題,但兩者的代碼都已附加。Android應用程序無法打開網頁鏈接

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?我沒有正確定義意圖嗎?

+0

通過'按鈕你的意思broken'它沒有做任何事情? – gunar

+0

是的。不過,好消息是應用程序並沒有完全崩潰。 – javathunderman

+0

你有Andrea的答案。 – gunar

回答

2

您需要添加這對您的onCreate()方法:

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