1

我制作了一个程序,我在主要活动中制作了一个按钮,然后单击它,这是一个新活动,它将给我一个 web 视图。这是我的 mainactivity.java 代码

public class ButtonWebActivity extends Activity {


@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      


Button z=(Button)findViewById(R.id.button1);

z.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(),WebLinks.class);
            startActivity(intent);

    }
});
}
}

这是我的第二个 activity.java 代码:

     public class WebLinks extends Activity {
 @Override

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.weblinks);

    WebView wv=(WebView)findViewById(R.id.webview1);

     WebSettings ws=wv.getSettings();
        ws.setBuiltInZoomControls(true);

        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new Callback());
        wv.loadUrl("http://www.google.com");

    }
private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        return(false);
    }
}
   }

这是我的 LogCat-

 07-06 09:22:50.626: D/AndroidRuntime(271): Shutting down VM
 07-06 09:22:50.626: W/dalvikvm(271): threadid=1: thread exiting with uncaught   exception (group=0x4001d800)
 07-06 09:22:50.656: E/AndroidRuntime(271): FATAL EXCEPTION: main
 07-06 09:22:50.656: E/AndroidRuntime(271): java.lang.IllegalStateException: Could not execute method of the activity
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.view.View$1.onClick(View.java:2072)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.view.View.performClick(View.java:2408)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.view.View$PerformClick.run(View.java:8816)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.os.Handler.handleCallback(Handler.java:587)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.os.Handler.dispatchMessage(Handler.java:92)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.os.Looper.loop(Looper.java:123)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at java.lang.reflect.Method.invokeNative(Native Method)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at java.lang.reflect.Method.invoke(Method.java:521)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at dalvik.system.NativeStart.main(Native Method)
 07-06 09:22:50.656: E/AndroidRuntime(271): Caused by: java.lang.reflect.InvocationTargetException
 07-06 09:22:50.656: E/AndroidRuntime(271):     at web.on.buttons.ButtonWebActivity.onClick(ButtonWebActivity.java:32)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at java.lang.reflect.Method.invokeNative(Native Method)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at java.lang.reflect.Method.invoke(Method.java:521)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.view.View$1.onClick(View.java:2067)
 07-06 09:22:50.656: E/AndroidRuntime(271):     ... 11 more
 07-06 09:22:50.656: E/AndroidRuntime(271): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {web.on.buttons/net.open.sports.WebLinks}; have you declared this activity in your AndroidManifest.xml?
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.app.Activity.startActivityForResult(Activity.java:2817)
 07-06 09:22:50.656: E/AndroidRuntime(271):     at android.app.Activity.startActivity(Activity.java:2923)
 07-06 09:22:50.656: E/AndroidRuntime(271):     ... 15 more

当我按下应用程序中的按钮时,出现“web.on.buttons 已意外停止。请稍后再试”的对话框。谁能告诉我我哪里出错了?谢谢。

4

2 回答 2

0

更改您的代码

 public void onClick(View v) {
     Intent intent = new Intent(getBaseContext(),WebLinks.class);
     startActivity(intent);
 }

对此:

public void onClick(View v) {
    Intent intent = new Intent(ButtonWebActivity.this,WebLinks.class);
    startActivity(intent);
}

调用意图时始终使用 Activity 的上下文。

于 2013-07-04T15:59:14.407 回答
0

请试试这个

z.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            startActivity(new Intent(ButtonWebActivity.this, WebLinks.class)); 

        }
   });
于 2013-07-04T15:39:06.160 回答