Weird App not working on different mobile

275 views Asked by At

I develop a Google Map related App on Samsung Mobile and release in play store which shows approx 7000 supported devices. When i downloaded the App on Samsung mobile it's working fine but not working on Moto and Sony Mobiles.

When i try to debug with Moto mobile it's shows "Shutting Down VM"

06-20 00:17:25.276      457-457/com.mycompany.mypackage D/AndroidRuntime﹕ Shutting down VM
06-20 00:17:25.281      457-457/com.mycompany.mypackage E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.mypackage, PID: 457
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mycompany.mypackage/com.mycompany.mypackage.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:131)
        at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
        at android.location.Geocoder.<init>(Geocoder.java:83)
        at com.mycompany.mypackage.MapsActivity.<init>(MapsActivity.java:96)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1572)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)

            

1

There are 1 answers

4
Sajal Mittal On BEST ANSWER

Answer: To the above weird problem,

I encounter very weird problem as discuss above which i finally figure out due to the Geocoder. Solution of above problem is very simple as shown below

Geocoder gc = new Geocoder(this, Locale.getDefault());

public class MapsActivity extends FragmentActivity implements .... {
...............
Geocoder gc = new Geocoder(this, Locale.getDefault());
@Override
protected void onCreate(Bundle savedInstanceState) {
............
      }
............
}

In the above code snippet Geocoder defined globally. This doesn't work on different devices. Works only on Samsung devices. The solution which i figure out finally works on all devices (Tested on Samsung, Moto, Sony).

public class MapsActivity extends FragmentActivity implements .... {
...............
@Override
protected void onCreate(Bundle savedInstanceState) {
............
      }
public void send(View view) {
    try {
        Geocoder gc = new Geocoder(this, Locale.getDefault());
        List<Address> list = gc.getFromLocationName(message, 1);
.........}
    }
............
}

In above code snippet Geocoder defined locally inside the function.

This solution works for me. I hope in future someone encounter the same problem may this solution helps.

Thanks