3

我正在尝试在 android 中生成自定义 tabView

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

解决此问题的任何输入 谢谢

4

1 回答 1

3

问题出在这一行:

Photos=(TabHost) findViewById(R.id.PhotoButton);

我假设那R.id.PhotoButton是 a Button,而您正试图将其转换为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)
于 2013-10-03T13:40:13.927 回答