你好,我正在尝试使用tabactive,但似乎无法使其工作。我试图将MainActivity扩展到TabActivity,但它似乎已被弃用。
我现在使用的代码如下:
package jdexamples.app13_exploringtabs;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// tabhost
TabHost mTabHost = (TabHost) findViewById(R.id.tabHost);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this ,first.class )));
mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , second.class )));
mTabHost.addTab(mTabHost.newTabSpec("third").setIndicator("Third").setContent(new Intent(this, third.class)));
mTabHost.setCurrentTab(0);
}
}
关于如何解决这个问题有什么建议吗?
我尝试了TabHost中Android Maps的建议。getTabHost()返回错误,但当我尝试在模拟器上启动应用程序时,它总是崩溃。
logcat如下:
10-16 22:09:01.375 11571-11571/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-16 22:09:01.375 11571-11571/? I/art﹕ Late-enabling JIT
10-16 22:09:01.377 11571-11571/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-16 22:09:01.537 11571-11571/jdexamples.app13_exploringtabs W/System﹕ ClassLoader referenced unknown path: /data/app/jdexamples.app13_exploringtabs-2/lib/x86
10-16 22:09:01.565 11571-11571/jdexamples.app13_exploringtabs D/AndroidRuntime﹕ Shutting down VM
10-16 22:09:01.565 11571-11571/jdexamples.app13_exploringtabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: jdexamples.app13_exploringtabs, PID: 11571
java.lang.RuntimeException: Unable to start activity ComponentInfo{jdexamples.app13_exploringtabs/jdexamples.app13_exploringtabs.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setup()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setup()' on a null object reference
at jdexamples.app13_exploringtabs.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-16 22:09:05.029 11571-11571/? I/Process﹕ Sending signal. PID: 11571 SIG: 9
首先启用
setContentView(R.layout.activity_main);
TabActivity这个类在API级别13中已弃用。新应用程序应该使用Fragments而不是这个类;要继续在旧设备上运行,可以使用v4支持库。
坏方法
使用已弃用的代码。
切换到FragmentActivity或AppCompatActivity
看看这里
>
如何使用现有的FragmentTabHost添加滑动选项卡功能?
Android应用程序不断与tabhost崩溃
http://developer.android.com/intl/es/reference/android/widget/TabHost.html#setup(android.app. LocalActivityManager)
我建议你使用PagerSlidingTabs。在这里查看此链接
通过使用上述库,您可以使用ViewPager滑动页面。您可以在此处查看Play商店的工作示例。希望这将对您有所帮助。
Android现在提供了在您的应用程序中实现选项卡的新方法,这些选项卡使用起来非常简单……
首先,您必须向项目添加依赖项:
compile 'com.android.support:design:22.2.1'
之后,您必须在活动的xml文件中声明您的tabLayout:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFC107"
app:tabMinWidth="30dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#FFFFFF" />
现在在你的活动课上做这样的事情:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(your_view_pager);