我从教程中学习后尝试创建Tab View。但每当我运行应用程序时,它都会崩溃。我把代码放在这里。立即需要帮助
package com.example.androidtablayoutactivity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
// setting Title and Icon for the Tab
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
}
布局文件= activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
SongsActivity.class
package com.example.androidtablayoutactivity;
import android.app.Activity;
import android.os.Bundle;
public class SongsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_layout);
}
}
songs_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for the SONGS -->
<TextView android:text="SONGS HERE"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
logcat的:
05-23 17:39:52.411: D/ActivityThread(20945): setTargetHeapConcurrentStart:2097152
05-23 17:39:52.741: D/AndroidRuntime(20945): Shutting down VM
05-23 17:39:52.761: W/dalvikvm(20945): threadid=1: thread exiting with uncaught exception (group=0x41744438)
05-23 17:39:52.781: E/AndroidRuntime(20945): FATAL EXCEPTION: main
05-23 17:39:52.781: E/AndroidRuntime(20945): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidtablayoutactivity/com.example.androidtablayoutactivity.MainActivity}: java.lang.ClassNotFoundException: com.example.androidtablayoutactivity.MainActivity
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.ActivityThread.access$700(ActivityThread.java:143)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.os.Looper.loop(Looper.java:137)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.ActivityThread.main(ActivityThread.java:4963)
05-23 17:39:52.781: E/AndroidRuntime(20945): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 17:39:52.781: E/AndroidRuntime(20945): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 17:39:52.781: E/AndroidRuntime(20945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-23 17:39:52.781: E/AndroidRuntime(20945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-23 17:39:52.781: E/AndroidRuntime(20945): at dalvik.system.NativeStart.main(Native Method)
05-23 17:39:52.781: E/AndroidRuntime(20945): Caused by: java.lang.ClassNotFoundException: com.example.androidtablayoutactivity.MainActivity
05-23 17:39:52.781: E/AndroidRuntime(20945): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-23 17:39:52.781: E/AndroidRuntime(20945): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-23 17:39:52.781: E/AndroidRuntime(20945): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
05-23 17:39:52.781: E/AndroidRuntime(20945): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
05-23 17:39:52.781: E/AndroidRuntime(20945): ... 11 more
答案 0 :(得分:0)
请看这个简单的例子,它会帮助你
活动名称:
public class TabHostActivity extends Activity {
/** Called when the activity is first created. */
Button btn1, btn2;
TabHost tabs;
TabHost.TabSpec spec;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabs = (TabHost) findViewById(R.id.TabHost01);
tabs.setup();
Button btn1 = (Button) tabs.getCurrentView()
.findViewById(R.id.Button01);
Button btn2 = (Button) tabs.getCurrentView()
.findViewById(R.id.Button02);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
spec = tabs.newTabSpec("tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("Clock");
tabs.addTab(spec);
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
spec = tabs.newTabSpec("tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Button");
tabs.addTab(spec);
}
});
tabs.setCurrentTab(0);
}
}
XML内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/tabcontent1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@+id/TabHost01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/Button01"
android:layout_width="160dip"
android:layout_height="65dip"
android:text="CLOCK" >
</Button>
<Button
android:id="@+id/Button02"
android:layout_width="160dip"
android:layout_height="65dip"
android:text="BUTTON" >
</Button>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="62px" >
<AnalogClock
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</AnalogClock>
<Button
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi_random button" >
</Button>
</FrameLayout>
</TabHost>
</FrameLayout>
</LinearLayout>