2011-05-29 26 views
5

जावा और एंड्रॉइड को समझने की कोशिश करना एक बटन पर क्लिक करने के बाद उपयोगकर्ता ब्राउज़र खोलने के एक साधारण कार्य में मदद करना चाहता है।वेब पेज पर ब्राउज़र खोलें एंड्रॉइड ऐप

मैं पिछले दो दिनों से ट्यूटोरियल कर रहा हूं हालांकि यह मदद कर सकता है अगर मैंने अभी इसमें एक स्टैब लिया और प्रतिक्रिया प्राप्त की। किसी भी सहायता के लिए अग्रिम रूप से धन्यवाद।

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

+1

इसके अलावा, 'AbsoluteLayout' से छुटकारा पाएं। उस कंटेनर वर्ग को बहिष्कृत किया गया है। कुछ और प्रयोग करें। – CommonsWare

+0

@trutheality कोई कोड एमुलेटर में नहीं चलाएगा। मैं अगली पोस्ट में अपनी त्रुटियों को पोस्ट करना सुनिश्चित कर दूंगा। – Denoteone

उत्तर

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); 
        } 
        } 
     }); 
    } 
} 

(तुम भी निश्चित रूप से, /res/drawable/ में एक bgimage2.png फ़ाइल और /res/values/strings.xml में एक start स्ट्रिंग की जरूरत है)।

+0

मेरी मदद करने के लिए समय निकालने के लिए धन्यवाद, मैं दोनों की तुलना करूंगा और पता लगाऊंगा कि मैं क्या खो रहा था। – Denoteone

+1

सभी अच्छे, लेकिन आप एक टोस्ट दिखाते हैं: .show() – LEX

+0

आह, धन्यवाद - मैंने उस पर स्किम किया क्योंकि यह सवाल का हिस्सा नहीं था। –

4

आसान बनाने के लिए आप

आशय आशय = नए आशय कर सकता है (Intent.ACTION_VIEW, Uri.parse ("http://www.yahoo.com")); प्रारंभ गतिविधि (इरादा);

संबंधित मुद्दे