Java 在android中设置简单的自定义选项卡 我正在尝试在android中生成一个自定义选项卡视图

Java 在android中设置简单的自定义选项卡 我正在尝试在android中生成一个自定义选项卡视图,java,android,android-tabhost,Java,Android,Android Tabhost,AndroidTabRestaurantDescListView.java public class AndroidTabRestaurantDescListView extends TabActivity { // TabSpec Names private static final String INBOX_SPEC = "Rating"; private static final String OUTBOX_SPEC = "Price";

AndroidTabRestaurantDescListView.java

public class AndroidTabRestaurantDescListView extends TabActivity {
    // TabSpec Names
        private static final String INBOX_SPEC = "Rating";
        private static final String OUTBOX_SPEC = "Price";
        private static final String PROFILE_SPEC = "Distance";
        private TabHost Photos;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Photos=(TabHost) findViewById(R.id.PhotoButton); 
            TabHost tabHost = getTabHost();
            Photos.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

            // Inbox Tab
            TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
            Intent inboxIntent = new Intent(this, MainActivity.class);
            inboxSpec.setIndicator(INBOX_SPEC);
            // Tab Content
            inboxSpec.setContent(inboxIntent);

            // Outbox Tab
            TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
            Intent PriceIntent = new Intent(this, PriceDescriptionActivity.class);
            PriceSpec .setIndicator(OUTBOX_SPEC);
            PriceSpec.setContent(PriceIntent);

            // Profile Tab
            TabSpec DistanceSpec = tabHost.newTabSpec(PROFILE_SPEC);
            Intent DistanceIntent = new Intent(this, DistanceDiscriptionActivity.class);
            DistanceSpec .setIndicator(PROFILE_SPEC); 
            DistanceSpec.setContent(DistanceIntent);

            // Adding all TabSpec to TabHost
            tabHost.addTab(inboxSpec); 
            tabHost.addTab(PriceSpec); 
            tabHost.addTab(DistanceSpec); 

            //Set the current value tab to default first tab
            tabHost.setCurrentTab(0);

            //Setting custom height for the tabs
            final int height = 45;
            tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
            tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
            tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;

            Photos.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent PhotoIntent = new Intent(AndroidTabRestaurantDescListView.this, AndroidTabRestaurantDescImageListView.class);
                    startActivity(PhotoIntent);             
                }
            });
        }
}
日志

10-03 18:49:34.710: D/AndroidRuntime(561): Shutting down VM
10-03 18:49:34.710: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-03 18:49:34.740: E/AndroidRuntime(561): FATAL EXCEPTION: main
10-03 18:49:34.740: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.jsonparsetutorial/com.project.findmybuffet.AndroidTabRestaurantDescListView}: java.lang.ClassCastException: android.widget.Button
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.os.Looper.loop(Looper.java:123)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 18:49:34.740: E/AndroidRuntime(561):  at java.lang.reflect.Method.invokeNative(Native Method)
10-03 18:49:34.740: E/AndroidRuntime(561):  at java.lang.reflect.Method.invoke(Method.java:507)
10-03 18:49:34.740: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 18:49:34.740: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 18:49:34.740: E/AndroidRuntime(561):  at dalvik.system.NativeStart.main(Native Method)
10-03 18:49:34.740: E/AndroidRuntime(561): Caused by: java.lang.ClassCastException: android.widget.Button
10-03 18:49:34.740: E/AndroidRuntime(561):  at com.project.findmybuffet.AndroidTabRestaurantDescListView.onCreate(AndroidTabRestaurantDescListView.java:27)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-03 18:49:34.740: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-03 18:49:34.740: E/AndroidRuntime(561):  ... 11 more
解决此问题的任何输入
谢谢

问题出在这一行:

Photos=(TabHost) findViewById(R.id.PhotoButton);
我假设
R.id.PhotoButton
是一个
按钮
,您试图将其强制转换到
TabHost
,但您不能这样做

一切基本上都可以在日志中看到。这两条线最为相关:

10-03 18:49:34.740: E/AndroidRuntime(561): Caused by: java.lang.ClassCastException: android.widget.Button
10-03 18:49:34.740: E/AndroidRuntime(561):  at com.project.findmybuffet.AndroidTabRestaurantDescListView.onCreate(AndroidTabRestaurantDescListView.java:27)

选项卡主机
没有单击事件。无法设置单击事件。有关更多详细信息,请查看教程:AndroidTabRestaurantDescListView的onCreate方法的第27行?@wyoskibum。。。。。线路是。。。Photos=(TabHost)findviewbyd(R.id.PhotoButton);如果R.id.PhotoButton是一个按钮小部件,那么您不能将case(TabHost)添加到视图中。你能把你的布局添加到问题中吗?