ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用TabWidgets的Android 2.1 NullPointerException

2019-06-30 20:10:53  阅读:189  来源: 互联网

标签:android tabs tabactivity tabwidget


我有一个我无法弄清楚的问题,它只发生在运行< 2.1的设备上.它在Android 2.2上运行良好.我有一个同步任务,在加载所有选项卡时显示加载对话框.以下是TabActivity的代码:

public class OppTabsView extends TabActivity {
Dialog dialog;
String errorText;
boolean save;
final int OPP_SAVE = 0;
public static boolean edited;

public void onCreate(Bundle icicle) {

    try {
        super.onCreate(icicle);
        new DoInBackground().execute();

    } catch (Exception e) {
        Toast.makeText(this, "Error occured. Please try again later.",
                Toast.LENGTH_SHORT).show();

    }

}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onStop() {
    super.onStop();

}

@Override
protected void onPause() {
    super.onPause();

}

public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, OPP_SAVE, 0, "Test");
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case OPP_SAVE:
        save = true;
        new DoInBackground().execute();

        return true;

    }

    return false;

}

public void LoadOpp() {
    handler.sendEmptyMessage(0);
}

public void SaveOpp() {
    DoStuff();

}

public void LoadLayout() {
    setContentView(R.layout.view_opptabs);

    /* TabHost will have Tabs */
    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

    /*
     * TabSpec used to create a new tab. By using TabSpec only we can able
     * to setContent to the tab. By using TabSpec setIndicator() we can set
     * name to tab.
     */

    /* tid1 is firstTabSpec Id. Its used to access outside. */
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");

    /* TabSpec setIndicator() is used to set name for the tab. */
    /* TabSpec setContent() is used to set content for a particular tab. */

    firstTabSpec.setIndicator("General",
            getResources().getDrawable(R.drawable.tab_moneybag))
            .setContent(new Intent(this, OppTabGeneral.class));
    secondTabSpec.setIndicator("Details",
            getResources().getDrawable(R.drawable.tab_papers)).setContent(
            new Intent(this, OppTabDetails.class));
    thirdTabSpec.setIndicator("Contact",
            getResources().getDrawable(R.drawable.tab_contact)).setContent(
            new Intent(this, OppTabContact.class));

    /* Add tabSpec to the TabHost to display. */
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);

}

private void do_update() {
    if (save) {
        SaveOpp();
    } else {
        LoadOpp();
    }
}

Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        LoadLayout();
    }
};

private class DoInBackground extends AsyncTask<Void, Void, Void> implements
        DialogInterface.OnCancelListener {

    protected void onPreExecute() {

        String verb = "Connecting";
        if (save) {
            verb = "Saving";
        }
        dialog = ProgressDialog.show(OppTabsView.this, "", verb
                + ".  Please Wait...", true, true, this);
    }

    protected Void doInBackground(Void... v) {
        do_update();
        return null;
    }

    protected void onPostExecute(Void v) {
        dialog.dismiss();


    }

    public void onCancel(DialogInterface dialog) {
        cancel(true);
        dialog.dismiss();
        finish();
    }
}
}

这是错误的堆栈跟踪:

java.lang.NullPointerException
at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
at android.view.View.draw(View.java:6538)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
at android.view.View.draw(View.java:6538)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
at android.view.ViewRoot.draw(ViewRoot.java:1349)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)

我试过踩过它,但错误似乎是从没有,而不是在特定的行.任何帮助是极大的赞赏.

解决方法:

TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");

您是否可以为多个TabSpec使用相同的“标签”?我会尝试正确设置它们,看它是否修复它.

编辑:好的,所以我的建议没有解决它,但无论如何都有独特的标签是有道理的.

试试这个看看它是否有帮助.添加一个选项,在LoadLayout()方法的末尾设置当前选中的选项卡,如下所示(参见最后一行)…

/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);

tabHost.setCurrentTab(0); // <== Add this

EDIT2:我发现TabWidget.java源代码和206行(发生NullPointerException)是……

mBottomLeftStrip.setState(selectedChild.getDrawableState());

……我可以看到有三种可能的异常原因.

> mBottonLeftStrip为空(极不可能)
> selectedCHild为null(TabWidget应该默认为子0并且使用tabHost.setCurrentTab()无论如何都会强制执行)
> selectedChild.getDrawableState()的结果为null

最后似乎是可能的原因,但我不确定是什么原因导致它返回null.

尝试谷歌’TabWidget.java源’ – 第二个结果点在grepcode.com上有行号,你可以看到它在异常点尝试做什么.

标签:android,tabs,tabactivity,tabwidget
来源: https://codeday.me/bug/20190630/1340093.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有