当我全局声明latlong值时获取零点异常

时间:2015-09-29 08:38:49

标签: java android

我使用共享首选项获取latlong值并将其存储为double值 全局声明latlong值。
获得零点异常。 尝试了在全球范围内宣布价值观的不同方式,但似乎没有任何效果。

MainActivity.java

public class MapsActivityConnect extends FragmentActivity {
ImageView emerg;
SharedPreferences pref;
String vechile;
Double deslatituded, deslongituded, srclatituded, srclongituded;
private GoogleMap mMap = null;
private final LatLng end = new LatLng(deslatituded, deslongituded);
private final LatLng start = new LatLng(srclatituded, srclongituded);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_maps_activity_connect);

    pref = getSharedPreferences("gps", Context.MODE_PRIVATE);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        deslatituded = extras.getDouble("deslatitude");
        deslongituded = extras.getDouble("deslongitude");


        srclatituded = extras.getDouble("srclatitude");
        srclongituded = extras.getDouble("srclongitude");
        vechile = extras.getString("vechile");

        if (!Utils.isConnected(getApplicationContext())) {
            Toast.makeText(getApplicationContext(), "Internet not available. Cross check your internet connectivity and try again", Toast.LENGTH_LONG).show();
            return;
        }
        if (!Utils.isGPSTurnOn(getApplicationContext())) {
            showGPSDialog();
            return;
        }
    }

}

@SuppressLint("NewApi")
@Override
protected void onResume() {
    super.onResume();
    if (Utils.isConnected(getApplicationContext())) {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMyLocationEnabled(true);
        final TextView txtDistance = (TextView) findViewById(R.id.txtSpeed);
        new Routing(getParent(), mMap, txtDistance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start, end);
    }
}

private void showGPSDialog() {
    new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppBaseTheme)) // Theme
            .setTitle(R.string.gps_lable_gps) // setTitle
            .setMessage(R.string.gps_lable_warning_message) // setMessage
            .setInverseBackgroundForced(false).setCancelable(false) //
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
                }
            }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            dialog.dismiss();
        }
    }).setIcon(android.R.drawable.ic_dialog_alert).show();
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Utils.isGPSTurnOn(getApplicationContext())) {
        onResume();
    }
}

}         logcat的

9-29 13:40:56.383  32078-    32149/zybo.example.ramz.demo_location_tracking V/RenderScript﹕ Application   requested CPU execution
09-29 13:40:56.393  32078-  32149/zybo.example.ramz.demo_location_tracking V/RenderScript﹕ 0xb7579cd8 Launching thread(s), CPUs 4
09-29 13:41:01.527  32078-32078/zybo.example.ramz.demo_location_tracking D/AndroidRuntime﹕ Shutting down VM
09-29 13:41:01.536  32078-32078/zybo.example.ramz.demo_location_tracking E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: zybo.example.ramz.demo_location_tracking, PID: 32078
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{zybo.example.ramz.demo_location_tracking/zybo.example.ramz.demo_location_tracking.MapsActivityConnect}: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        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:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
        at zybo.example.ramz.demo_location_tracking.MapsActivityConnect.<init>(MapsActivityConnect.java:62)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1089)
        at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)                                              
  at 
  android.app.ActivityThread.access$800(ActivityThread.java:155)

5 个答案:

答案 0 :(得分:5)

您需要使用值初始化。

Double deslatituded, deslongituded, srclatituded, srclongituded;

您声明了双打,但这些是对象,而不是基元。因此,默认值为null。并且取消装箱试图获得原始的

null.doubleValue()

将它们设为原始值或指定默认值,如:

Double desLatituded = new Double(0);

答案 1 :(得分:0)

我不确定你的错误是什么。但其中一个场景是 你声明了类变量

private final LatLng end = new LatLng(deslatituded, deslongituded);
private final LatLng start = new LatLng(srclatituded, srclongituded);

在这里,去除了长期值,必须为空。也许这就是nullPoiner的原因请检查一下。

您需要为此传递值。或者只是初始化诸如deslatituded = 0之类的值。这将使您免于崩溃

答案 2 :(得分:0)

试试这个(并确保你的LatLng变量不是最终的):

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_maps_activity_connect);

pref = getSharedPreferences("gps", Context.MODE_PRIVATE);

Bundle extras = getIntent().getExtras();
if (extras != null) {
    deslatituded = extras.getDouble("deslatitude");
    deslongituded = extras.getDouble("deslongitude");


    srclatituded = extras.getDouble("srclatitude");
    srclongituded = extras.getDouble("srclongitude");
    // NEW CODE
    end = new LatLng(deslatituded, deslongituded);
    start = new LatLng(srclatituded, srclongituded);
    // END NEW CODE
    vechile = extras.getString("vechile");

    if (!Utils.isConnected(getApplicationContext())) {
        Toast.makeText(getApplicationContext(), "Internet not available. Cross check your internet connectivity and try again", Toast.LENGTH_LONG).show();
        return;
    }
    if (!Utils.isGPSTurnOn(getApplicationContext())) {
        showGPSDialog();
        return;
    }
}

}

答案 3 :(得分:0)

请检查你的演员是否有钥匙&#34; deslatitude&#34;

喜欢:

 if(getIntent().hasCategory("deslatitude"))
  {
 deslatituded = extras.getDouble("deslatitude");
  }


Log.e(TAG, "deslatitude: " + deslatituded );

如果你的日志打印了valuew,那么检查一下(deslat,deslong,srclat,srclong)

答案 4 :(得分:0)

中是否有任何值?
     extras.getDouble("deslatitude");
     extras.getDouble("deslongitude");
     extras.getDouble("srclatitude");
     extras.getDouble("srclongitude");

没有任何意图附加内容。