简体   繁体   English

应用在模拟器上崩溃,但可以在真实设备上运行

[英]App crashes on emulator but it works on real device

So I have an application, which of course have a main layout. 所以我有一个应用程序,它当然具有主布局。 This layout have views which then I find them in activity -"findViewById". 此布局具有视图,然后我可以在活动中找到它们-“ findViewById”。 As usual I first setContentView and then I find views. 像往常一样,我先setContentView,然后找到视图。 Strange thing is that app works fine on my real device (Htc Desire 2.3.7 ), and on Samsung Galaxy S3 mini (4.1), but it instantly crashes on emulators -tested on 2.3.5 and 4.1 emulator. 奇怪的是,该应用程序可以在我的真实设备(Htc Desire 2.3.7)和Samsung Galaxy S3 mini(4.1)上正常运行,但是它立即在模拟器上崩溃-在2.3.5和4.1模拟器上测试。 The crash problem is very interesting: 崩溃问题非常有趣:

    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

NullPointerException? 空指针异常? What ? 什么 ? I've checked again if maybe I forgot to use "findViewById" method on some of the views, but I didn't. 我再次检查了是否忘了在某些视图上使用“ findViewById”方法,但我没有。 It works great on real device also. 它在真实设备上也很好用。

Anyone have ever met similar problem? 有人遇到过类似的问题吗?

Main Activity: 主要活动:

@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); selectedCategoriesPreference = getSharedPreferences(“ MyPrefs”,MODE_PRIVATE);

initializeViewsAndGetPreferences(); initializeViewsAndGetPreferences();

 }


private void initializeViewsAndGetPreferences() {

    recentTV = (TextView) findViewById(R.id.recent_TV);
    recentTV.setTypeface(PAPYRUS);
    recentTV.setOnClickListener(this);

} }

Here is my mainLayout xml ( only text view ) 这是我的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" />

I've figured out what was wrong.. 我已经找出了问题所在。

When testing on real device, screen size on mentioned phones above was medium - xlarge. 在真实设备上进行测试时,上述手机的屏幕尺寸为中等-xlarge。 I had two layouts. 我有两种布局。 One for medium - xlarge devices and one for small devices. 一种用于中型-xlarge设备,另一种用于小型设备。 When testing on emulator, I was using only small devices, therefore layout for small devices was used. 在模拟器上进行测试时,我仅使用小型设备,因此使用了小型设备的布局。 WHen testing on real devices, because of they have large screens, the other layout was pulled. 在真实设备上进行测试时,由于它们具有大屏幕,因此取消了其他布局。

And what was wrong with the small layout? 小布局有什么问题? Well, I'm a dumbass. 好吧,我是笨蛋。 I've added some new features into my application and I was working on main layout, and completely forgot about small layout. 我已经在应用程序中添加了一些新功能,并且正在处理主布局,而完全忘记了小布局。 So some views were not existing in small layout. 因此,某些视图在较小的布局中不存在。 That's why NullPointerException was thrown.. 这就是抛出NullPointerException的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM