折线在模拟器上崩溃
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了折线在模拟器上崩溃相关的知识,希望对你有一定的参考价值。
我正在尝试在地图上绘制折线
导入com.google.android.gms.maps.model.Polyline;导入com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList;导入java.util.List;
公共类MainActivity扩展了AppCompatActivity,它实现了OnMapReadyCallback,SeekBar.OnSeekBarChangeListener {//初始化变量
GoogleMap gMap;
SeekBar seekWidth, seekBlue, seekGreen, seekRed;
Button btClear, btDraw;
Polyline polyline = null;
List<LatLng> latLngList = new ArrayList<>();
List<Marker> markerList = new ArrayList<>();
PolylineOptions polylineOptions = null;
int red = 0, green= 0, blue=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assign variable
seekWidth = findViewById(R.id.seek_width);
seekRed = findViewById(R.id.seek_red);
seekGreen = findViewById(R.id.seek_green);
seekBlue = findViewById(R.id.seek_blue);
btDraw=findViewById(R.id.bt_draw);
btClear=findViewById(R.id.bt_clear);
/*Initialise SupportMapFragment
SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.google_map);
supportMapFragment.getMapAsync(this);
btDraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Draw Polyline on Map
if (polyline!=null) polyline.remove();
//Create PolylineOptions
PolylineOptions polylineOptions = new PolylineOptions()
.addAll(latLngList).clickable(true);
polyline = gMap.addPolyline(polylineOptions);
setWidth();
}
});*/
//Initialise SupportMapFragment
SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.google_map);
supportMapFragment.getMapAsync(this);
btDraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Draw Polyline on Map
if (polyline!=null) polyline.remove();
if(polylineOptions!=null){
//Create PolylineOptions
polyline = gMap.addPolyline(polylineOptions);
setWidth();
}
}
});
btClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Clear All
if (polyline!=null) polyline.remove();
for(Marker marker: markerList) marker.remove();
latLngList.clear();
markerList.clear();
seekWidth.setProgress(3);
seekBlue.setProgress(0);
seekGreen.setProgress(0);
seekRed.setProgress(0);
}
});
seekRed.setOnSeekBarChangeListener(this);
seekGreen.setOnSeekBarChangeListener(this);
seekBlue.setOnSeekBarChangeListener(this);
}
private void setWidth() {
seekWidth.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Get Seekbar Progress
int width = seekWidth.getProgress();
if (polyline != null) {
//Set Polyline Width
polyline.setWidth(width);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
/*@Override
public void onMapReady(GoogleMap googleMap) {
/*gMap= googleMap;
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Create MarkerOptions
MarkerOptions markerOptions = new MarkerOptions().position(latLng);
//Create Marker
Marker marker = gMap.addMarker(markerOptions);
//Add Latlng and Marker
latLngList.add(latLng);
markerList.add(marker);
}
});
}*/
public void onMapReady(GoogleMap googleMap) {
gMap= googleMap;
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Create MarkerOptions
MarkerOptions markerOptions = new MarkerOptions().position(latLng);
//Create Marker
Marker marker = gMap.addMarker(markerOptions);
//Add Latlng and Marker
latLngList.add(latLng);
markerList.add(marker);
polylineOptions = new PolylineOptions().addAll(latLngList).clickable(true);
}
});
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (seekBar.getId()){
case R.id.seek_red:
red = i;
break;
case R.id.seek_green:
green = i;
break;
case R.id.seek_blue:
blue= i;
break;
}
if(polyline !=null){
//Set Polyline Color
polyline.setColor(Color.rgb(red,green,blue));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
错误如下:] >>
2019-11-29 13:39:40.391 20694-20694 / com.example.poly E / AndroidRuntime:致命异常:主要流程:com.example.poly,PID:20694java.lang.NullPointerException:尝试在计算机上调用虚拟方法'void com.google.android.gms.maps.model.Polyline.setColor(int)'空对象引用
在com.example.poly.MainActivity.onProgressChanged(MainActivity.java:149)在android.widget.SeekBar.onProgressRefresh(SeekBar.java:93)在android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:1327)在android.widget.ProgressBar.refreshProgress(ProgressBar.java:1382)在android.widget.ProgressBar.setProgressInternal(ProgressBar.java:1447)在android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:850)在android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:760)在android.view.View.dispatchTouchEvent(View.java:9943)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)在com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411)在com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)在android.app.Activity.dispatchTouchEvent(Activity.java:3061)在androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)在com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373)在android.view.View.dispatchPointerEvent(View.java:10163)在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434)在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3849)在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3902)在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3868)在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3995)在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3876)在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4052)在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3849)在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3902)在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3868)在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3876)在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3849)在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184)在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145)在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313)在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)在android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native方法)在android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)在android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6284)在android.view.ViewRootImpl $ ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6336)
我正在尝试在地图导入com.google.android.gms.maps.model.Polyline上绘制折线;导入com.google.android.gms.maps.model.PolylineOptions;导入java.util.ArrayList;导入java.util ....
这实际上可能是临时修复程序或永久修复程序,具体取决于折线为何仍为null的根本原因,但下面应该解决您的崩溃问题。
以上是关于折线在模拟器上崩溃的主要内容,如果未能解决你的问题,请参考以下文章
每当我运行我的片段时,这行代码 mapFragment.setRetainInstance(true);正在崩溃我的应用程序? [关闭]