android非平凡findViewById NullPointerException
我有日历库,其中有CalendarViewActivity。android非平凡findViewById NullPointerException,android,android-layout,button,nullpointerexception,Android,Android Layout,Button,Nullpointerexception,我有日历库,其中有CalendarViewActivity。 我的项目使用此库并扩展CalendarViewActivity以设置自定义活动标题。 日历活动: public class CalendarActivity extends CalendarViewActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceStat
我的项目使用此库并扩展CalendarViewActivity以设置自定义活动标题。
日历活动:
public class CalendarActivity extends CalendarViewActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set custom activity title
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
TextView title = (TextView) getWindow().findViewById(R.id.txt_header);
if (isGo) {
title.setText(getResources().getString(R.string.title_date_go));
} else {
title.setText(getResources().getString(R.string.title_date_go_back));
}
}
}
在一些设备中(确切地说是:华硕tf101、三星Galaxy Gio S5660),我在这一行中得到了NullPointerException:
super.onCreate(savedInstanceState);
此引用库中的此行(此处显示异常):
Xml布局(库中的CalendarViewActivity):
我在清单中移动了屏幕方向的定义,这解决了我的问题。问题可能与getWindow()或其他与窗口相关的方法有关,因为它因设备而异。请参阅,如果发布堆栈跟踪,则在getWindow()之前会出现异常,也许有人可以告诉您出了什么问题。是的,但可能正是因为这样,not savebundledinstance才不会抛出错误。该CalendarViewActivity是否有多个xml布局文件?与该设备对应的xml布局(取决于配置)可能没有按钮,或者没有提供id参数?
((Button) findViewById(R.id.btn_today)).setOnClickListener(this);
<?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"
android:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/calendar_header"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/prevMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/descr_btn_prev_month"
android:src="@drawable/calendar_left_arrow_selector" >
</ImageView>
<TextView
android:id="@+id/currentMonth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" >
</TextView>
<ImageView
android:id="@+id/nextMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/descr_btn_next_month"
android:src="@drawable/calendar_right_arrow_selector" >
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" >
<GridView
android:id="@+id/calendarHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@android:color/transparent"
android:numColumns="7" >
</GridView>
<GridView
android:id="@+id/calendar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/calendar_bg"
android:listSelector="@android:color/transparent"
android:numColumns="7">
</GridView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btn_today"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/calendar_button"
android:text="@string/btn_today"
android:gravity="center"
android:textColor="@android:color/white"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<Button
android:id="@+id/btn_tomorrow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/calendar_button"
android:text="@string/btn_tomorrow"
android:gravity="center"
android:textColor="@android:color/white"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.package.package/net.package.package.ui.activities.CalendarActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.metalex.calendarview.UI.Activities.CalendarViewActivity.onCreate(CalendarViewActivity.java:118)
at net.aviascanner.aviascanner.ui.activities.CalendarActivity.onCreate(CalendarActivity.java:20)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
java.lang.NullPointerException
at com.metalex.calendarview.UI.Activities.CalendarViewActivity.onCreate(CalendarViewActivity.java:118)
at net.aviascanner.aviascanner.ui.activities.CalendarActivity.onCreate(CalendarActivity.java:20)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)