问题描述
我一直在努力奋斗着这个几天了。我想在一个按钮,点击程序中切换标签页。我的程序完美的作品,如果我只是用卡来更改活动,但接线一个onClick方法setCurrentTab导致错误。这是行不通的方法。这是一个pretty的基本和简单的功能,但我还没有看到太多的文件或人试图连接一个buttonclick与切换标签的例子。谢谢你。
的ImageButton下一=(的ImageButton)findViewById(R.id.ButtonAsk);
next.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图查看)
{
TabHost tabHost =(TabHost)findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(2);
}
});
这是错误日志:
12-23 15:39:21.439:ERROR / AndroidRuntime(327):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
12-23 15:39:21.459:ERROR / AndroidRuntime(327):显示java.lang.NullPointerException
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.example.tabssample.HomeActivity $ 1.onClick(HomeActivity.java:38)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.View.performClick(View.java:2364)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.View.onTouchEvent(View.java:4179)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.View.dispatchTouchEvent(View.java:3709)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.os.Handler.dispatchMessage(Handler.java:99)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.os.Looper.loop(Looper.java:123)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在android.app.ActivityThread.main(ActivityThread.java:4363)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在java.lang.reflect.Method.invokeNative(本机方法)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在java.lang.reflect.Method.invoke(Method.java:521)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-23 15:39:21.459:ERROR / AndroidRuntime(327):在dalvik.system.NativeStart.main(本机方法)
下面是你可以把你的onClick内的code例子()。这是马克和凯文描述。
TabActivity标签=(TabActivity)的getParent();
tabs.getTabHost()setCurrentTab(2)。
我用这个code珍闻无数次。希望这澄清。
I have been struggling with this for a few days now. I'm trying to switch tabs programmatically upon a button click. My program works flawlessly if I just use the tabs to change activities, but wiring an onClick method with setCurrentTab results in an error. This is the method that will not work. It's a pretty basic and straightforward function but I haven't seen much documentation or examples of people attempting to wire a buttonclick with switching tabs. Thanks.
ImageButton next = (ImageButton) findViewById(R.id.ButtonAsk);
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(2);
}
});
This is the error log:
12-23 15:39:21.439: ERROR/AndroidRuntime(327): Uncaught handler: thread main exiting due to uncaught exception
12-23 15:39:21.459: ERROR/AndroidRuntime(327): java.lang.NullPointerException
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.example.tabssample.HomeActivity$1.onClick(HomeActivity.java:38)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.View.performClick(View.java:2364)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.View.onTouchEvent(View.java:4179)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.View.dispatchTouchEvent(View.java:3709)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.os.Looper.loop(Looper.java:123)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at java.lang.reflect.Method.invoke(Method.java:521)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-23 15:39:21.459: ERROR/AndroidRuntime(327): at dalvik.system.NativeStart.main(Native Method)
Here's a code example that you can put inside your onClick(). It's as Mark and Kevin described.
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(2);
I've used this code tidbit numerous times. Hope this clarifies.
这篇关于如何在程序中切换在Android中使用buttonclick标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!