rootpanthera
rootpanthera

Reputation: 2771

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". As usual I first setContentView and then I find views. 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. 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. 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);

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 )

<TextView
    android:id="@+id/recent_TV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_marginLeft="200dp"
    android:text="Recent" />

Upvotes: 1

Views: 825

Answers (1)

rootpanthera
rootpanthera

Reputation: 2771

I've figured out what was wrong..

When testing on real device, screen size on mentioned phones above was medium - xlarge. I had two layouts. One for medium - xlarge devices and one for small devices. 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..

Upvotes: 2

Related Questions