使用android.R.id.title时获取NullPointer异常

时间:2013-12-06 13:54:32

标签: android nullpointerexception textview

我正在尝试使用android.r.id.title这样的textview来显示我的标签名称 但它在2.2上抛出了NullPointerException,但它在>=4.x上工作正常。

这是我的代码供参考。

for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {     
            mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height /= 2;
            Log.i("TESTING WIDTH OF TABS ", ""
                    + mTabHost.getTabWidget().getChildAt(i).getWidth());
            View tabView = getTabWidget().getChildTabViewAt(i);

            View v = (View) tabView.getParent();
            tv = (TextView) v.findViewById(android.R.id.title);
            Typeface faceBold = Typeface.createFromAsset(getAssets(),
                    "fonts/eau_sans_bold.otf");
            tv.setTypeface(faceBold);
            tv.setTextSize(12);
            tv.setGravity(Gravity.CENTER);
            tv.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
            tv.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;

            tv.setGravity(Gravity.CENTER_VERTICAL);
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundColor(Color.TRANSPARENT);   

这是我的logcat。

12-06 19:22:38.764: E/AndroidRuntime(603): FATAL EXCEPTION: main
12-06 19:22:38.764: E/AndroidRuntime(603): java.lang.RuntimeException: Unable to start activity ComponentInfo{com./com..ProfileActivity}: java.lang.NullPointerException
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.os.Looper.loop(Looper.java:123)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 19:22:38.764: E/AndroidRuntime(603):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 19:22:38.764: E/AndroidRuntime(603):  at java.lang.reflect.Method.invoke(Method.java:521)
12-06 19:22:38.764: E/AndroidRuntime(603):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 19:22:38.764: E/AndroidRuntime(603):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 19:22:38.764: E/AndroidRuntime(603):  at dalvik.system.NativeStart.main(Native Method)
12-06 19:22:38.764: E/AndroidRuntime(603): Caused by: java.lang.NullPointerException
12-06 19:22:38.764: E/AndroidRuntime(603):  at com..ProfileActivity.onCreate(ProfileActivity.java:471)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 19:22:38.764: E/AndroidRuntime(603):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我不明白为什么我会得到这个例外 请给我任何提示或参考。

1 个答案:

答案 0 :(得分:0)

尝试更改代码逻辑并使用{​​{3}}方法更改选项卡名称。您可以启动TextView programmaticaly(避免findViewById)并将其设置为此方法的参数。这样,您的代码对于您描述的错误更安全,因为您独立于不同Android版本中的视图层次结构更改。