每次点击Go的时候,程序就会崩溃。
代码如下:包括:
/src/.../BrowserIntentActivity.java
/res/layout/main.xml
/res/strings/string.xml
AndroidManifest.xml未改动package com.payne.android.BrowserActivity;import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.KeyEvent;
import android.view.View.OnKeyListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class BrowserIntentActivity extends Activity {
private EditText urlText;
private Button goButton; /** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Get a handle to all user interface elements
        urlText = (EditText) findViewById(R.id.url_field);
        goButton = (Button) findViewById(R.id.go_button);
        
        // Setup event handlers
        goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
openBrowser();
}
});

urlText.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View view, int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_ENTER){
openBrowser();
return true;
}
return false;
}
});
    } /** Open a browser on the URL specified in the text box */
private void openBrowser() {
Uri uri = Uri.parse(urlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText 
     android:id="@+id/url_field"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1.0"
     android:lines="1"
     android:inputType="textUri"
     android:imeOptions="actionGo"/>
    <Button 
     android:id="@+id/go_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/go_button"/>
</LinearLayout>/res/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="go_button">Go</string>
    <string name="app_name">BrowserIntent</string>
</resources>

解决方案 »

  1.   

    你在输入框中输入的是什么?测了下, 在输入框输入 http://www.google.com 可以连接到谷歌。
      

  2.   

    但是我运行的时候,不论输入什么,我输入的也是"www.google.com“,也会崩溃掉。
      

  3.   

    <uses-permission android:name="android.permission.INTERNET" /> 
    要这个权限把,
    怎么之前回的看不见?
      

  4.   

    你的代码来看没有问题,要是程序崩溃,就是你没有访问intent的权限,加权限吧。在AndroidManifest.xml中加上可以访问intent的权限。
      

  5.   

    AndroidManifest.xml未改动 
    估计是没加权限<uses-permission android:name="android.permission.INTERNET" />  
      

  6.   

    最后发现问题好像是没有加http://
    权限也改过来了
    另外,为什么不加http://就不能呢,并且还会崩溃,有什么办法可以做到不加http://吗
      

  7.   

    private void openBrowser() {
            String httpHead = "http://";
            String urlStr = urlText.getText().toString());
            if(!urlStr.startWidth(httpHead))
              urlStr = httpHead + urlStr;
            Uri uri = Uri.parse(urlStr);        
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
      

  8.   


    正解+1private void openBrowser() {
           String httpHead = "http://";
           String urlStr = (urlText.getText().toString());
           if(!urlStr.startsWith(httpHead))
           urlStr = httpHead + urlStr;
           Uri uri = Uri.parse(urlStr);   
           Intent intent = new Intent(Intent.ACTION_VIEW, uri);
           startActivity(intent);
        
           }
      

  9.   

    很明显是权限问题,看看DDMS里面的错误日志