我创建了一个程序,我在主要活动中创建一个按钮,然后单击它,这将是一个新的活动,它将为我提供webview。 这是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意外停止。请稍后再试。” 谁能告诉我哪里出错了? 感谢。
答案 0 :(得分:0)
请试试这个
z.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent(ButtonWebActivity.this, WebLinks.class));
}
});
答案 1 :(得分: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的上下文。