0

在构建了一个基本的计算器之后,我正在尝试构建一个 android 应用程序,该应用程序可以打开在 android 浏览器的编辑文本字段中设置的 URL。从概念上讲,这听起来很容易,但对我来说,它远非如此。

我的java代码

package com.example;


import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;



public class Access extends Activity implements OnClickListener {

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

        Button btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);

    }

    public void onClick(View view){
        EditText browse =(EditText)findViewById(R.id.btn1);
        Intent i = new Intent("android.intent.action.VIEW", Uri.parse(browse.getText().toString()));
        startActivity(i);

    }
}

我的 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"
        >
    <TextView
            android:id="@+id/url"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Enter webpage to go"
            />
    <EditText
            android:id="@+id/btn1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            />

    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/radioGroup1"
            android:onClick="onClick"
            android:text="Browse"
            />
</LinearLayout>

还有我的 logcat 错误

09-28 11:31:13.615: ERROR/StrictMode(92): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
        java.lang.Throwable: Explicit termination method 'close' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:184)
        at java.io.FileInputStream.<init>(FileInputStream.java:80)
        at com.android.server.pm.UserManager.readUserList(UserManager.java:114)
        at com.android.server.pm.UserManager.<init>(UserManager.java:81)
        at com.android.server.pm.UserManager.<init>(UserManager.java:85)
        at com.android.server.pm.PackageManagerService.<init>(PackageManagerService.java:923)
        at com.android.server.pm.PackageManagerService.main(PackageManagerService.java:833)
        at com.android.server.ServerThread.run(SystemServer.java:163)
09-28 11:31:21.256: ERROR/SoundPool(92): error loading /system/media/audio/ui/Lock.ogg
09-28 11:31:21.256: ERROR/SoundPool(92): error loading /system/media/audio/ui/Unlock.ogg
09-28 11:31:21.435: ERROR/EventHub(92): could not get driver version for /dev/input/mouse0, Not a typewriter
09-28 11:31:21.465: ERROR/EventHub(92): could not get driver version for /dev/input/mice, Not a typewriter
09-28 11:31:21.814: ERROR/CommandListener(33): Failed to open /proc/sys/net/ipv6/conf/wlan0/disable_ipv6: No such file or directory
09-28 11:31:21.814: ERROR/WifiStateMachine(92): Failed to disable IPv6: java.lang.IllegalStateException: Unable to communicate to native daemon for disabling ipv6
09-28 11:31:21.904: ERROR/MobileDataStateTracker(92): default: Ignoring feature request because could not acquire PhoneService
09-28 11:31:21.904: ERROR/MobileDataStateTracker(92): default: Could not enable APN type "default"
09-28 11:31:30.305: ERROR/ActivityThread(185): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
09-28 11:31:30.315: ERROR/BinaryDictionaryGetter(185): Could not find a dictionary pack
09-28 11:31:39.365: ERROR/SoundPool(92): error loading /system/media/audio/ui/Effect_Tick.ogg
09-28 11:31:39.365: ERROR/SoundPool(92): error loading /system/media/audio/ui/Effect_Tick.ogg
09-28 11:31:39.365: ERROR/SoundPool(92): error loading /system/media/audio/ui/Effect_Tick.ogg
09-28 11:31:39.404: ERROR/SoundPool(92): error loading /system/media/audio/ui/Effect_Tick.ogg
09-28 11:31:39.404: ERROR/SoundPool(92): error loading /system/media/audio/ui/Effect_Tick.ogg
09-28 11:31:39.404: ERROR/SoundPool(92): error loading /system/media/audio/ui/KeypressStandard.ogg
09-28 11:31:39.414: ERROR/SoundPool(92): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-28 11:31:39.414: ERROR/SoundPool(92): error loading /system/media/audio/ui/KeypressDelete.ogg
09-28 11:31:40.074: ERROR/SoundPool(92): error loading /system/media/audio/ui/KeypressReturn.ogg
09-28 11:32:01.754: ERROR/DefaultVoicemailNotifier(291): No voicemails to notify about: clear the notification.
09-28 11:32:23.674: ERROR/AndroidRuntime(583): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Access}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
        at com.example.Access.onCreate(Access.java:26)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
        ... 11 more
4

1 回答 1

1

改变你的

 Button btn=(Button) findViewById(R.id.btn);

 Button btn=(Button) findViewById(R.id.button1);
于 2012-09-28T06:14:45.130 回答