java-在android中设置简单的自定义标签
作者:互联网
我正在尝试在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
解决此问题的任何输入
谢谢
解决方法:
问题在这一行:
Photos=(TabHost) findViewById(R.id.PhotoButton);
我假设R.id.PhotoButton是一个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)
标签:android-tabhost,java,android 来源: https://codeday.me/bug/20191122/2061692.html