我第一次尝试使用TabHost而且我遇到了一些问题。 知道为什么这不起作用吗?
activity_dashboard.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="ro.softwarex.bellaapp.testtabhost.app.DashboardActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:textColor="#33b5e5"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="@string/dummy_content" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="ro.softwarex.bellaapp.testtabhost.app.main">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
android:layout_gravity="center_horizontal|top">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
和java ...: DashboardActivity.java
package ro.softwarex.bellaapp.testtabhost.app;
import ro.softwarex.bellaapp.testtabhost.app.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class DashboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
final Button blogin = (Button) findViewById(R.id.dummy_button);
blogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(DashboardActivity.this, main.class);
startActivity(i);
Toast mytoast = Toast.makeText(getApplicationContext(), "Wow it works", Toast.LENGTH_SHORT);
mytoast.show();
}
});
}
}
和 main.java
package ro.softwarex.bellaapp.testtabhost.app;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TabHost;
public class main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
}
}
}
当我单击第一个活动中的按钮时,我应该看到tabHost的第二个活动,但我得到的是一条消息“不幸的是,testtabapp停止工作”
我没有继续设置tabhost,因为它给出了相同的消息,所以我将代码剥离到了它停止工作的位置
我做错了什么?
(PS。:我正在使用Android Studio) 我试图替换TabHost的id,但即使在LogCat中也会出现相同的结果。 我不明白这个问题。你在自己的环境中测试它,看看你是否得到了相同的结果?
替换TabHost布局中的id后,应用程序不会崩溃,但LogCat中的错误与之前相同。它仍然抱怨tabHost ID,但没有崩溃。
另外,如果我想通过添加选项卡来继续代码,应用程序会崩溃同样的LogCat?!?!?!抱怨tabHost id,但没有停止应用程序。
所以代码添加:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Toast mytoast = Toast.makeText(getApplicationContext(), "It's greater than HONEYCOMB", Toast.LENGTH_SHORT);
mytoast.show();
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("TabClienti");
TabHost.TabSpec tab2 = tabHost.newTabSpec("TabVizite");
TabHost.TabSpec tab3 = tabHost.newTabSpec("TabRaportZi");
TabHost.TabSpec tab4 = tabHost.newTabSpec("TabSync");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Clientii");
tab1.setContent(new Intent(this,ListClientiTab.class));
tab2.setIndicator("Vizitele");
tab2.setContent(new Intent(this,ListViziteTab.class));
tab3.setIndicator("Raport zi");
tab3.setContent(new Intent(this,RaportZiTab.class));
tab4.setIndicator("Sincronizare");
tab4.setContent(new Intent(this, SyncTab.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
tabHost.setup();
(这被添加到将TabHost保存在其布局中的活动的onCreate中。)
此代码的LogCat(我从上面的LogCat中删除了错误,所以下面只显示了点击按钮时会发生什么):
03-05 18:13:44.709 1460-1460/ro.softwarex.bellaapp.testtabhost.app D/AndroidRuntime﹕ Shutting down VM
03-05 18:13:44.709 1460-1460/ro.softwarex.bellaapp.testtabhost.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-05 18:13:44.749 1460-1460/ro.softwarex.bellaapp.testtabhost.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.softwarex.bellaapp.testtabhost.app/ro.softwarex.bellaapp.testtabhost.app.main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.TabHost.addTab(TabHost.java:232)
at ro.softwarex.bellaapp.testtabhost.app.main.onCreate(main.java:45)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
03-05 18:13:47.178 1460-1460/ro.softwarex.bellaapp.testtabhost.app I/Process﹕ Sending signal. PID: 1460 SIG: 9
答案 0 :(得分:3)
问题似乎与您TabHost
的命名有关。你把它命名为:
android:id="@+id/tabHost"
但是当您创建Activity
文件时,您正在寻找:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
这与你的不一样。您的TabHost
应该这样命名:
android:id="@android:id/tabhost"
答案 1 :(得分:0)
我将此添加为答案,以便其他人可以将其用作解决方案。但是我会接受nKn的回答,因为他是解决最初问题的最有帮助的。
所以我找到的解决方案是:
替换public class main extends Activity {
public class main extends TabActivity {
(TabActivity在我的编辑器中有一个删除线,表示已弃用)
然后,为了访问tabHost,而不是这样做:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);...
我这样做了:
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("clientii").setIndicator(
"TAB clienti").setContent(new Intent(this,ListClientiTab.class)));
所以,像这样,我可以看到TABS的活动,并且应用程序不会崩溃。 我讨厌这种只能通过使用弃用的aproaches(快速)解决的问题。