2013-07-24 38 views
0

我很難識別是什麼導致此應用程序崩潰。editText解析雙重

當我註釋掉我的行以從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 
+1

張貼堆棧跟蹤。 – Raghunandan

回答

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(); 
1

findViewById需要的意見目前hiearchy爲了工作要創建,這樣你就可以調用

EditText length = (EditText) findViewById(R.id.editText1); 
String lengthString = length.getText().toString(); 

後才setContentView

+0

@Raghunandan好的。謝謝。 它現在一直運行,直到地圖加載並暫停。 我會發布上面編輯的代碼。 – johnsonjp34

+0

發佈堆棧跟蹤。 – Blackbelt

+0

以上是我的意思嗎? – johnsonjp34