2011-05-29 64 views
5

試圖掌握Java和Android想要幫助他們點擊一個按鈕後打開用戶瀏覽器的簡單任務。打開瀏覽器到網頁安卓應用

我一直在做最後兩天的教程,雖然它可能有幫助,如果我只是刺傷它並獲得反饋。在此先感謝您的幫助。

的main.xml:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bgimage2"> 
    > 

<Button 
android:id="@+id/goButton" 
android:layout_width="150px" 
android:layout_height="wrap_content" 
android:text="@string/start" 
android:layout_x="80px" 
android:layout_y="21px" 
> 
</AbsoluteLayout> 

GetURL.java:

package com.patriotsar; 

import android.app.Activity; 
import android.content.Intent; 
import android.view.View.OnClickListener; 

String url = "http://www.yahoo.com"; 
Intent i = new Intent(Intent.ACTION_VIEW); 
Uri u = Uri.parse(url); 
i.setData(u); 


public class patriosar extends Activity { 

    private Button goButton; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     goButton.setOnClickListener(new OnClickListener() {   
      @Override 
      public void onClick(View v){ 
       try { 
         // Start the activity 
         startActivity(i); 
        } catch (ActivityNotFoundException e) { 
         // Raise on activity not found 
         Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT); 
        } 
        } 
     }); 

    } 
} 
+2

那麼......問題是什麼?代碼是否工作?代碼是否失敗?怎麼了? – trutheality 2011-05-29 07:59:13

+1

另外,擺脫'AbsoluteLayout'。該容器類已被棄用。使用別的東西。 – CommonsWare 2011-05-29 10:59:11

+0

@trutheality代碼不會在模擬器中運行。我會確保在下一篇文章中發佈我的錯誤。 – Denoteone 2011-05-29 22:26:21

回答

6

已經很接近了,但幾件事情是在錯誤的地方或丟失。下面的代碼工作 - 我試圖做出最低限度的必要更改。您可以將兩個版本加載到WinMerge之類的東西中,以確切瞭解更改的內容。

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bgimage2" 
    > 

    <Button 
     android:id="@+id/goButton" 
     android:layout_width="150px" 
     android:layout_height="wrap_content" 
     android:text="@string/start" 
     android:layout_x="80px" 
     android:layout_y="21px" 
    ></Button> 
</LinearLayout> 

GetURL.java:

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class GetURL extends Activity { 
    private Button goButton; 
    String url = "http://www.yahoo.com"; 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    Uri u = Uri.parse(url); 
    Context context = this; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     goButton = (Button)findViewById(R.id.goButton); 
     goButton.setOnClickListener(new OnClickListener() {   
      @Override 
      public void onClick(View v){ 
       try { 
         // Start the activity 
         i.setData(u); 
         startActivity(i); 
        } catch (ActivityNotFoundException e) { 
         // Raise on activity not found 
         Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT); 
        } 
        } 
     }); 
    } 
} 

(也當然需要一個bgimage2.png文件/res/drawable/start字符串/res/values/strings.xml)。

+0

感謝您花時間幫助我,我會比較兩者並找出我錯過的東西。 – Denoteone 2011-05-29 22:29:13

+1

所有的好,但你忘了吐司:.show() – LEX 2012-10-05 14:46:25

+0

啊,謝謝 - 我撇了撇,因爲它不是問題的一部分。 – 2012-10-07 09:53:01

4

爲了簡化你可以做

意向意圖=新意圖(Intent.ACTION_VIEW,Uri.parse( 「http://www.yahoo.com」)); startActivity(intent);