使用Intent显示URL

Luf*_*ffy -1 android android-intent illegalstateexception

我在运行时收到错误.单击按钮时发生Logcat错误.

java.lang.IllegalStateException: Could not find a method onClickWebBrowser(View) in the activity class com.steph.intents.IntentActivity for onClick handler on view class android.widget.Button with id 'btn_webbrowser'

堆栈跟踪:

 E/AndroidRuntime(1931): FATAL EXCEPTION: main
 E/AndroidRuntime(1931): Process: com.steph.intents, PID: 1931
 E/AndroidRuntime(1931): java.lang.IllegalStateException: Could not find a method onClickWebBrowser(View) in the activity class com.steph.intents.IntentActivity for onClick handler on view class android.widget.Button with id 'btn_webbrowser'
 E/AndroidRuntime(1931):    at android.view.View$1.onClick(View.java:3810)
 E/AndroidRuntime(1931):    at android.view.View.performClick(View.java:4438)
 E/AndroidRuntime(1931):    at android.view.View$PerformClick.run(View.java:18422)
 E/AndroidRuntime(1931):    at android.os.Handler.handleCallback(Handler.java:733)
 E/AndroidRuntime(1931):    at android.os.Handler.dispatchMessage(Handler.java:95)
 E/AndroidRuntime(1931):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1931):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1931):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1931):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1931):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1931):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1931):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1931): Caused by: java.lang.NoSuchMethodException: onClickWebBrowser [class android.view.View]
 E/AndroidRuntime(1931):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
 E/AndroidRuntime(1931):    at java.lang.Class.getMethod(Class.java:857)
 E/AndroidRuntime(1931):    at android.view.View$1.onClick(View.java:3803)
 E/AndroidRuntime(1931):    ... 11 more
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_webbrowser"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Web Browser"
        android:onClick="onClickWebBrowser"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

IntentActivity.java:

package com.steph.intents;

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

public class IntentActivity extends Activity{

    Button btn_webbrowser;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        btn_webbrowser=(Button)findViewById(R.id.btn_webbrowser);

        setContentView(R.layout.activity_main);
    }

    public void onclickWebBrowser(View view){
        Intent i=new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.amazon.com"));
        startActivity(i);

    }

}     
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我.谢谢.

M D*_*M D 5

我认为你应该改变顺序

 setContentView(R.layout.activity_main); 
 btn_webbrowser=(Button)findViewById(R.id.btn_webbrowser);
Run Code Online (Sandbox Code Playgroud)

首先你需要setContentView(R.layout.activity_main);然后参考你的View.