所以我有一个应用程序,它当然有一个主布局。这个布局有视图,然后我在活动中找到它们 - “findViewById”。像往常一样,我首先设置内容视图,然后找到视图。奇怪的是,该应用程序在我的真实设备(Htc Desire 2.3.7)和三星 Galaxy S3 mini(4.1)上运行良好,但它在模拟器上立即崩溃 - 在 2.3.5 和 4.1 模拟器上测试。崩溃问题很有趣:
10-28 19:39:58.718: E/AndroidRuntime(5183): FATAL EXCEPTION: main
10-28 19:39:58.718: E/AndroidRuntime(5183): java.lang.RuntimeException: Unable to start activity ComponentInfo{wowmemes.iterbit.com/activites.iterbit.com.MainActivity}: java.lang.NullPointerException
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.os.Looper.loop(Looper.java:137)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.ActivityThread.main(ActivityThread.java:5039)
10-28 19:39:58.718: E/AndroidRuntime(5183): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 19:39:58.718: E/AndroidRuntime(5183): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 19:39:58.718: E/AndroidRuntime(5183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-28 19:39:58.718: E/AndroidRuntime(5183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-28 19:39:58.718: E/AndroidRuntime(5183): at dalvik.system.NativeStart.main(Native Method)
10-28 19:39:58.718: E/AndroidRuntime(5183): Caused by: java.lang.NullPointerException
10-28 19:39:58.718: E/AndroidRuntime(5183): at activites.iterbit.com.MainActivity.initializeViewsAndGetPreferences(MainActivity.java:200)
10-28 19:39:58.718: E/AndroidRuntime(5183): at activites.iterbit.com.MainActivity.onCreate(MainActivity.java:107)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.Activity.performCreate(Activity.java:5104)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-28 19:39:58.718: E/AndroidRuntime(5183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-28 19:39:58.718: E/AndroidRuntime(5183): ... 11 more
空指针异常?什么 ?我再次检查了是否可能忘记在某些视图上使用“findViewById”方法,但我没有。它也适用于真实设备。
有人遇到过类似的问题吗?
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
PAPYRUS = Typeface.createFromAsset(getAssets(), "fonts/papyrus.ttf");
if(!checkIfNetworkAvailable()) {
new MyCustomDialog(this, R.style.MyDialogStyle, R.layout.network_check, getResources().getString(R.string.networkCheck_Text)).show();
}
selectedCategoriesPreference =getSharedPreferences("MyPrefs",MODE_PRIVATE);
initializeViewsAndGetPreferences();
}
private void initializeViewsAndGetPreferences() {
recentTV = (TextView) findViewById(R.id.recent_TV);
recentTV.setTypeface(PAPYRUS);
recentTV.setOnClickListener(this);
}
这是我的 mainLayout xml(只有文本视图)
<TextView
android:id="@+id/recent_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="Recent" />