- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我注意到使用 tabhost 有很多问题,但没有一个真正适合我的问题。我正在尝试使用 action bar 创建 View 在顶部,然后是栏下方的一排选项卡。这是我的 Activity 布局。
<?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"
style="@style/Dashboard">
<com.ftni.common.ui.ActionBar
android:id="@+id/actionbar"
style="@style/ActionBar"/>
<TabHost 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"
android:padding="5dp">
<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"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
接下来,我们声明我的 Activity
public class DashboardActivity extends TabActivity {
我正在像这样构建我的标签。
private void buildTabs()
{
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, StatementsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("statements").setIndicator("Statements",
res.getDrawable(R.drawable.ic_tab_active))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, PaymentsActivity.class);
spec = tabHost.newTabSpec("payments").setIndicator("Payments",
res.getDrawable(R.drawable.ic_tab_active))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
当我尝试使用 getTabHost();
时它总是返回一个空值。所以,我切换到 TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
它不返回空 tabhost,而是 tabHost.addTab(spec);
仍然会导致空指针异常。
这是堆栈跟踪
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): FATAL EXCEPTION: main
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ftni.consumer/com.ftni.consumer.ui.DashboardActivity}: java.lang.NullPointerException
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.os.Looper.loop(Looper.java:123)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at java.lang.reflect.Method.invoke(Method.java:507)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at dalvik.system.NativeStart.main(Native Method)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): Caused by: java.lang.NullPointerException
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.widget.TabHost.addTab(TabHost.java:212)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.ftni.consumer.ui.DashboardActivity.buildTabs(DashboardActivity.java:75)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at com.ftni.consumer.ui.DashboardActivity.onCreate(DashboardActivity.java:29)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
04-26 19:50:12.494: ERROR/AndroidRuntime(7367): ... 11 more
第 75 行是第一个 tabHost.addTab(spec);
此外,我的所有 Activity 都在我的 list 中声明。
<activity android:name=".ui.DashboardActivity"></activity>
<activity android:name=".ui.StatementsActivity"></activity>
<activity android:name=".ui.PaymentsActivity"></activity>
编辑:
为了证明这不是问题,我删除了操作栏,将布局完全按照教程中的内容进行布局,但我仍然从 getTabHost();
返回了一个空值。
编辑 2:为清楚起见,完整的类代码
public class DashboardActivity extends TabActivity {
private ProfileModel profile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
profile = Controller.getProfile();
setContentView(R.layout.dashboard);
buildTabs();
new LoadDashboardTask().execute();
}
@Override
public void onContentChanged()
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
if (actionBar != null)
{
actionBar.setOnTitleClickListener(new OnClickListener() {
public void onClick(View v) {
//do nothing for now. User is already "home"
}
});
if(profile.Accounts.length == 1)
actionBar.setTitle(profile.Accounts[0].Name);
else
actionBar.setTitle(profile.Name);
}
}
private void buildTabs()
{
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();// The activity TabHost
if(tabHost != null)
{
// Initialize a TabSpec for each tab and add it to the TabHost
TabSpec statementSpec = tabHost.newTabSpec("statements").setIndicator("Statements",
res.getDrawable(R.drawable.ic_tabs_statements))
.setContent(new Intent().setClass(DashboardActivity.this, StatementsActivity.class));
tabHost.addTab(statementSpec);
TabSpec paymentSpec = tabHost.newTabSpec("payments").setIndicator("Payments",
res.getDrawable(R.drawable.ic_tabs_payments))
.setContent(new Intent().setClass(DashboardActivity.this, PaymentsActivity.class));
tabHost.addTab(paymentSpec);
tabHost.setCurrentTab(0);
}
}
private class LoadDashboardTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute()
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
actionBar.setProgressBarVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params)
{
return null;
}
@Override
protected void onPostExecute(Void result)
{
ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
actionBar.setProgressBarVisibility(View.GONE);
}
}
}
最佳答案
文档指出您应该“如果使用 findViewById() 加载 TabHost,则在添加选项卡之前调用 setup()。”,这正是您正在做的。参见 here .但是,既然您使用的是 TabActivity,为什么不调用 getTabHost()
而不是手动获取 View ?
关于android - 当 tabhost 不是布局中唯一的东西时,Tabhost nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5795677/
我正在将我的模板代码移植到 XTend。在某些时候,我在测试用例中有这种类型的条件处理: @Test def xtendIfTest() { val obj = new FD if (
我是新来的 kotlin , 当我开始 Null Safety 时,我对下面的情况感到困惑. There's some data inconsistency with regard to initia
我的应用程序一直在各种Android版本中保持良好状态。我有用户在Android 4.3、5.0、5.1和6.0上正常运行。但是,具有S7 Edge的用户刚刚更新了Android 7.0,将文本粘贴到
我使用的是最新版本的 LWUIT (1.5)。我在资源编辑器中设计了我的表单,然后将代码生成到 netbeans。问题是如果我想访问除表单之外的任何对象,我会收到此错误: java.lang.Null
更新: 我在 Fedora 21 上运行它。 SonarQube - 5.0。 SonarQube Runner - 2.4 更新 2:Findbugs v3.1,Java 插件 v2.8 更新3:
RecupData 我的类仅在 web 中返回 NullPointerException。我连接到 pgsql db 8.3.7 - 该脚本在“控制台”syso 中运行良好 - 但引发了测试 Web
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
每次运行此 jsp 时,都会收到以下错误异常: org.apache.jasper.JasperException: java.lang.NullPointerException root cause
Kotlin 在编译时有一个出色的 null 检查,使用分离到“可空?”和“不可为空”的对象。它有一个 KAnnotator 来帮助确定来自 Java 的对象是否可以为空。但是,如果 not-null
我有一个布局将显示一个TextView,用于显示一个滴答时间。我遵循了此链接中的代码 How to Display current time that changes dynamically for
Elasticsearch 1.4.1版(“lucene_version”:“4.10.2”) 我有一个像这样的文件: $ curl 'http://localhost:9200/blog/artic
这是我从另一个类调用函数的方法Selenium 设置已定义。 public void Transfer() throws Exception { System.out.println("\nTrans
我试图在主类中使用我在此类中创建的函数,但它崩溃并显示“警告:无法在根 0 处打开/创建首选项根节点 Software\JavaSoft\Prefsx80000002。 Windows RegCrea
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 3 年前。 我有一个 Java 代码,它将
我声明了两张牌: Card card1 = new Card('3', Card.Suit.clubs); Card card2 = new Card('T', Card.Suit.diamonds)
我编写了一段代码来解码 Base64 图像并在 javafx 中表示该图像。在我的 url base64 代码中不断变化。这就是我在 javafx 代码中使用任务的原因。但我收到错误:java.lan
我正在尝试使用 arrayList 的 arrayList 在 Java 中实现图形。 每当调用 addEdge 函数时,我都会收到 NullPointerException 。我似乎无法弄清楚为什么
我是 Java/android 的新手,所以很多这些术语都是外国的,但我愿意学习。我不打算详细介绍该应用程序,因为我认为它不相关。我目前的问题是,我使用了博客中的教程和代码 fragment ,并使我
我正在开发一个 Android 应用程序来在 Android developer guide 的帮助下录制视频.我程序上的所有代码都与此页面相同。 我在 之外定义了权限标签。 当应
我是一名优秀的程序员,十分优秀!