Java 全局声明latlong值时获取空点异常
我使用共享首选项获取latlong值,并将其存储为双值。Java 全局声明latlong值时获取空点异常,java,android,Java,Android,我使用共享首选项获取latlong值,并将其存储为双值。 全局声明的latlong值。 获取空点异常。 尝试了不同的方法在全球范围内声明值,但似乎没有任何效果 MainActivity.java public class MapsActivityConnect extends FragmentActivity { ImageView emerg; SharedPreferences pref; String vechile; Double deslatituded, deslongituded,
全局声明的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)
9-29 13:40:56.383 32078-32149/zybo.example.ramz.demo\u location\u tracking V/RenderScript﹕ 应用程序请求的CPU执行
09-29 13:40:56.393 32078-32149/zybo.example.ramz.demo_location_tracking V/RenderScript﹕ 0xb7579cd8启动线程,CPU 4
09-29 13:41:01.527 32078-32078/zybo.example.ramz.demo_location_tracking D/AndroidRuntime﹕ 关闭虚拟机
09-29 13:41:01.536 32078-32078/zybo.example.ramz.demo\u location\u tracking E/AndroidRuntime﹕ 致命异常:主
进程:zybo.example.ramz.demo_location_tracking,PID:32078
java.lang.RuntimeException:无法实例化活动组件信息{zybo.example.ramz.demo_location_tracking/zybo.example.ramz.demo_location_tracking.MapsactivityConnection}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double java.lang.double.double.doubleValue()”
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
在android.app.ActivityThread.access$800(ActivityThread.java:155)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5343)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:905)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“double java.lang.double.doubleValue()”
在zybo.example.ramz.demo_location_tracking.MapsActivityConnect.(MapsActivityConnect.java:62)
位于java.lang.reflect.Constructor.newInstance(本机方法)
位于java.lang.Class.newInstance(Class.java:1606)
位于android.app.Instrumentation.newActivity(Instrumentation.java:1089)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
在
access$800(ActivityThread.java:155)
您需要使用值进行初始化
Double deslatituded, deslongituded, srclatituded, srclongituded;
可以声明double,但它们是对象,而不是基本体。因此,默认值为null。而解装箱则试图得到类似于
null.doubleValue()
将其设置为基本值或指定默认值,如:
Double desLatituded = new Double(0);
我不确定你的错误是什么。但其中一种情况是 您可以声明类变量
private final LatLng end = new LatLng(deslatituded, deslongituded);
private final LatLng start = new LatLng(srclatituded, srclongituded);
此处的deslatituded、DesLongitudded值必须为null。也许这就是为什么会出现这种情况,请检查一下
您需要为此传递值。或者只是初始化一些值,比如deslatuded=0或其他什么。这将使您免于崩溃尝试以下操作(并确保您的LatLng变量不是最终变量):
}请检查您的额外服务是否有“deslatitude”键 比如:
if(getIntent().hasCategory("deslatitude"))
{
deslatituded = extras.getDouble("deslatitude");
}
Log.e(TAG, "deslatitude: " + deslatituded );
如果您的日志打印了值w,那么请检查所有值(deslat、deslong、srclat、srclong)中是否有任何值
extras.getDouble("deslatitude");
extras.getDouble("deslongitude");
extras.getDouble("srclatitude");
extras.getDouble("srclongitude");
intent extras中没有任何内容。好吧,LatLng是空的,因为您的参数(Double deslatuded、deslongitudded、srlatuded、srclogitudded;)此时为空……您从getIntent()获取值。getExtras()不是从shared获取值preference@Clairvoyant所以怎么了?谢谢大家的回答。实际上,我的代码运行良好。问题在于共享的首选项。我没有分享正确的价值观。一旦我修好了,它就开始工作了。:)嘿,它不起作用了。在本例中,我的地图的源和目标为0。请查找我的logcat输出“I/System.out﹕ sbuf:“当然,我的答案只与空指针有关。如何用真实的数据填充它不是我的职责:)谢谢。这有帮助
extras.getDouble("deslatitude");
extras.getDouble("deslongitude");
extras.getDouble("srclatitude");
extras.getDouble("srclongitude");