为什么这个小程序会崩溃~~求助~~

tsinghua_hp 2011-07-14 02:14:50
每次点击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>




...全文
364 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
EricFung 2011-07-14
  • 打赏
  • 举报
回复
学习了
leon3k 2011-07-14
  • 打赏
  • 举报
回复
很明显是权限问题,看看DDMS里面的错误日志
qiying1988 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 youngplayee 的回复:]
private void openBrowser() {
String httpHead = "http://";
String urlStr = urlText.getText().toString());
if(!urlStr.startWidth(httpHead))
urlStr = httpHead + urlStr;
Uri uri = Uri.parse……
[/Quote]

正解+1

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

}


jmlee 2011-07-14
  • 打赏
  • 举报
回复
前几天看过pro.android.2讲到
之所以要加上http:// 是因为
当startActivity 的时候android机制是先检查URI前面的标识符
是http的话就检查有没有INTERNET的权限 进而调用浏览器
tel就调用电话、geo就调用地图
youngplayee 2011-07-14
  • 打赏
  • 举报
回复
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);
}
tsinghua_hp 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qiying1988 的回复:]

http://www.google.com
[/Quote]


最后发现问题好像是没有加http://
权限也改过来了


另外,为什么不加http://就不能呢,并且还会崩溃,

有什么办法可以做到不加http://吗
xinqiqi123 2011-07-14
  • 打赏
  • 举报
回复
AndroidManifest.xml未改动
估计是没加权限

<uses-permission android:name="android.permission.INTERNET" />
小笨熊 2011-07-14
  • 打赏
  • 举报
回复
你的代码来看没有问题,要是程序崩溃,就是你没有访问intent的权限,加权限吧。在AndroidManifest.xml中加上可以访问intent的权限。
b87936260 2011-07-14
  • 打赏
  • 举报
回复
<uses-permission android:name="android.permission.INTERNET" />
要这个权限把,
怎么之前回的看不见?
qiying1988 2011-07-14
  • 打赏
  • 举报
回复
http://www.google.com
tsinghua_hp 2011-07-14
  • 打赏
  • 举报
回复
但是我运行的时候,不论输入什么,我输入的也是"www.google.com“,也会崩溃掉。
[Quote=引用 2 楼 qiying1988 的回复:]

你在输入框中输入的是什么?

测了下, 在输入框输入 http://www.google.com 可以连接到谷歌。
[/Quote]
qiying1988 2011-07-14
  • 打赏
  • 举报
回复
你在输入框中输入的是什么?

测了下, 在输入框输入 http://www.google.com 可以连接到谷歌。
dinjay 2011-07-14
  • 打赏
  • 举报
回复
把错误日志贴出来。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧