2011-05-29 65 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);