我无法确定导致此应用崩溃的原因。
当我注释掉我的行以从我的editText框中检索文本时,该应用程序将无法正常工作。
如果我只是将圆圈设置为程序中定义的半径,它看起来很好。
我注意到的行是我怀疑问题的地方。谢谢!
public class MainActivity extends Activity implements LocationListener{
GoogleMap mMap;
Location myLocation;
//EditText length = (EditText) findViewById(R.id.editText1);
//String lengthString = length.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap!= null) {
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomBy(17));}
Location myLocation;
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cr = new Criteria();
String provider = locationmanager.getBestProvider(cr, true);
Location location = locationmanager.getLastKnownLocation(provider);
locationmanager.requestLocationUpdates(provider, 200, 0, (LocationListener) this);
}
@SuppressLint("NewApi")
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
mMap.moveCamera(CameraUpdateFactory.newLatLng((new LatLng(location.getLatitude(), location.getLongitude()))));
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(location.getLatitude(), location.getLongitude()));
// double bacons = Double.parseDouble(lengthString);
// if (bacons >=0) {
//double radi = bacons * 0.3048;
//circleOptions.radius(radi); // In meters
//circleOptions.fillColor(0xffff0000);
//circleOptions.strokeWidth(0);
//mMap.addCircle(circleOptions);
// } else {
double radi = 20 * 0.3048;
circleOptions.radius(radi); // In meters
circleOptions.fillColor(0xffff0000);
circleOptions.strokeWidth(0);
mMap.addCircle(circleOptions); }
//}
好的,这是在完成以下建议后的做法。它现在运行,但一旦地图加载就会暂停。
public class MainActivity extends Activity implements LocationListener{
GoogleMap mMap;
Location myLocation;
EditText length;
String lengthString;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
length = (EditText) findViewById(R.id.editText1);
lengthString = length.getText().toString();
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap!= null) {
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomBy(17));}
// Get back the mutable Circle
Location myLocation;
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cr = new Criteria();
String provider = locationmanager.getBestProvider(cr, true);
Location location = locationmanager.getLastKnownLocation(provider);
locationmanager.requestLocationUpdates(provider, 200, 0, (LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
mMap.moveCamera(CameraUpdateFactory.newLatLng((new LatLng(location.getLatitude(), location.getLongitude()))));
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(location.getLatitude(), location.getLongitude()));
double bacons = Double.parseDouble(lengthString);
if (bacons >=0) {
double radi = bacons * 0.3048;
circleOptions.radius(radi); // In meters
circleOptions.fillColor(0xffff0000);
circleOptions.strokeWidth(0);
mMap.addCircle(circleOptions);
} else {
double radi = 20 * 0.3048;
circleOptions.radius(radi); // In meters
circleOptions.fillColor(0xffff0000);
circleOptions.strokeWidth(0);
mMap.addCircle(circleOptions); }
}
logcat
07-24 11:23:45.021: I/dalvikvm(18121): Debugger is active
07-24 11:23:45.211: I/System.out(18121): Debugger has connected
07-24 11:23:45.211: I/System.out(18121): waiting for debugger to settle...
07-24 11:23:45.411: I/System.out(18121): waiting for debugger to settle...
07-24 11:23:45.622: I/System.out(18121): waiting for debugger to settle...
07-24 11:23:45.822: I/System.out(18121): waiting for debugger to settle...
07-24 11:23:46.022: I/System.out(18121): waiting for debugger to settle...
07-24 11:23:46.222: I/System.out(18121): waiting for debugger to settle...
07-24 11:23:46.432: I/System.out(18121): debugger has settled (1318)
07-24 11:23:46.783: D/libEGL(18121): loaded /system/lib/egl/libEGL_adreno200.so
07-24 11:23:46.783: D/libEGL(18121): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
07-24 11:23:46.783: D/libEGL(18121): loaded /system/lib/egl/libGLESv2_adreno200.so
07-24 11:23:46.783: I/Adreno200-EGL(18121): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: (CL3579225)
07-24 11:23:46.783: I/Adreno200-EGL(18121): Build Date: 04/22/13 Mon
07-24 11:23:46.783: I/Adreno200-EGL(18121): Local Branch: adreno_sba_20130419
07-24 11:23:46.783: I/Adreno200-EGL(18121): Remote Branch:
07-24 11:23:46.783: I/Adreno200-EGL(18121): Local Patches:
07-24 11:23:46.783: I/Adreno200-EGL(18121): Reconstruct Branch:
07-24 11:23:46.823: D/OpenGLRenderer(18121): Enabling debug mode 0
07-24 11:23:52.739: D/GestureDetector(18121): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
07-24 11:23:53.920: D/AbsListView(18121): Get MotionRecognitionManager
07-24 11:23:53.960: D/AbsListView(18121): onVisibilityChanged() is called, visibility : 8
07-24 11:23:53.960: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:53.970: D/AbsListView(18121): onVisibilityChanged() is called, visibility : 8
07-24 11:23:53.970: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:53.980: D/dalvikvm(18121): GC_CONCURRENT freed 838K, 10% free 17854K/19680K, paused 4ms+4ms, total 41ms
07-24 11:23:54.110: D/AbsListView(18121): onVisibilityChanged() is called, visibility : 0
07-24 11:23:54.110: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:54.500: D/dalvikvm(18121): GC_FOR_ALLOC freed 187K, 9% free 17909K/19680K, paused 27ms, total 27ms
07-24 11:23:54.500: I/dalvikvm-heap(18121): Grow heap (frag case) to 18.505MB for 60496-byte allocation
07-24 11:23:54.530: D/dalvikvm(18121): GC_FOR_ALLOC freed <1K, 9% free 17967K/19740K, paused 23ms, total 23ms
07-24 11:23:54.590: D/dalvikvm(18121): GC_FOR_ALLOC freed 139K, 9% free 18210K/19980K, paused 18ms, total 18ms
07-24 11:23:54.921: D/AbsListView(18121): onVisibilityChanged() is called, visibility : 4
07-24 11:23:54.921: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:54.941: D/AbsListView(18121): onVisibilityChanged() is called, visibility : 0
07-24 11:23:54.941: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:55.021: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:55.551: D/dalvikvm(18121): GC_FOR_ALLOC freed 1664K, 14% free 18427K/21228K, paused 35ms, total 35ms
07-24 11:23:55.631: D/AbsListView(18121): unregisterIRListener() is called
07-24 11:23:56.562: D/dalvikvm(18121): GC_CONCURRENT freed 332K, 13% free 18540K/21228K, paused 4ms+4ms, total 30ms
07-24 11:23:56.562: D/dalvikvm(18121): WAIT_FOR_CONCURRENT_GC blocked 22ms
07-24 11:23:56.863: D/dalvikvm(18121): GC_CONCURRENT freed 269K, 12% free 18803K/21228K, paused 3ms+4ms, total 26ms
07-24 11:23:59.555: D/dalvikvm(18121): GC_CONCURRENT freed 521K, 12% free 18848K/21228K, paused 4ms+5ms, total 31ms
07-24 11:24:03.019: D/dalvikvm(18121): GC_CONCURRENT freed 562K, 12% free 18868K/21228K, paused 4ms+6ms, total 50ms
07-24 11:24:04.120: D/dalvikvm(18121): threadid=1: still suspended after undo (sc=1 dc=1)
07-24 11:24:04.140: D/dalvikvm(18121): threadid=1: still suspended after undo (sc=1 dc=1)
07-24 11:24:04.140: D/dalvikvm(18121): GC_CONCURRENT freed 332K, 10% free 19152K/21228K, paused 3ms+5ms, total 29ms
07-24 11:24:04.690: D/dalvikvm(18121): threadid=1: still suspended after undo (sc=1 dc=1)
07-24 11:24:04.690: D/dalvikvm(18121): GC_FOR_ALLOC freed 453K, 10% free 19301K/21228K, paused 22ms, total 22ms
07-24 11:24:04.700: I/dalvikvm-heap(18121): Grow heap (frag case) to 20.307MB for 524304-byte allocation
07-24 11:24:04.720: D/dalvikvm(18121): GC_FOR_ALLOC freed 136K, 10% free 19676K/21744K, paused 22ms, total 22ms
07-24 11:24:04.720: D/dalvikvm(18121): threadid=1: still suspended after undo (sc=1 dc=1)
07-24 11:24:05.131: D/dalvikvm(18121): threadid=1: still suspended after undo (sc=1 dc=1)
07-24 11:24:05.131: D/dalvikvm(18121): GC_FOR_ALLOC freed 564K, 12% free 19180K/21744K, paused 27ms, total 28ms
07-24 11:24:05.131: I/dalvikvm-heap(18121): Grow heap (frag case) to 20.689MB for 1048592-byte allocation
07-24 11:24:05.151: D/dalvikvm(18121): threadid=1: still suspended after undo (sc=1 dc=1)
07-24 11:24:05.151: D/dalvikvm(18121): GC_FOR_ALLOC freed 1K, 12% free 20203K/22772K, paused 23ms, total 23ms
答案 0 :(得分:1)
findViewById
需要创建当前的视图层次结构才能工作,因此您可以调用
EditText length = (EditText) findViewById(R.id.editText1);
String lengthString = length.getText().toString();
仅在setContentView
答案 1 :(得分:1)
您正在onCreate
之外进行初始化。在onCreate
内移动edittext的初始化。
EditText length;
String lengthString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
length = (EditText) findViewById(R.id.editText1);
....// rest of the code
}
还可以点击按钮上的文字
lengthString = length.getText().toString();