2012-11-24 38 views
-1

我在做一個Google地圖應用程序。
我寫了一個函數來查找地址。
當我在主要活動中編寫代碼時,它是可行的。
但是當我寫在另一個活動。它總是強制關閉應用程序。在另一個活動中查找地址

什麼是錯,我該如何解決它?

這是我的日誌:

11-24 08:56:52.659: E/AndroidRuntime(16334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zzzzzz/com.example.zzzzzz.Direction}: java.lang.NullPointerException 

這是我mainactivity代碼中找到地址:

 // find address 
    final Geocoder geocoder = new Geocoder(MainActivity.this); 

    final EditText ed = (EditText) findViewById(R.id.edAddress); 
    Button btAddress = (Button) findViewById(R.id.btAddress); 
    btAddress.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      String Ten = ed.getText().toString(); 
      try { 
       List<Address> l = geocoder.getFromLocationName(Ten, 5); 
       if (l != null && l.size() > 0) { 
        int lat = (int) (l.get(0).getLatitude() * 1000000); 
        int lon = (int) (l.get(0).getLongitude() * 1000000); 
        GeoPoint pt = new GeoPoint(lat, lon); 

        p = pt; 
        MapOverlay mar = new MapOverlay(); 
        listOfOverlays.add(mar); 

        mv.getController().setZoom(17); 
        mv.getController().setCenter(pt); 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       Log.v("Loi: ", "Loi 1 cua tui ne"); 
      } 
     } 
    }); 

,這是我的功能開始其他活動:

public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.about: 
     //about 
     Intent about = new Intent("com.example.zzzzzz.About"); 
     startActivity(about); 
     break; 
    case R.id.setting: 
     //setting 
     Intent setting = new Intent("com.example.zzzzzz.Setting"); 
     startActivity(setting); 
     break; 
    case R.id.direction: 
     //direction 
     Intent direction = new Intent("com.example.zzzzzz.Direction"); 
     startActivity(direction); 
     break; 
    case R.id.MyLocation: 
     //mylocation 
     //Intent mylocation = new Intent("com.example.zzzzzz.MyLocation"); 
     //startActivity(mylocation); 
     initLocationManagerV2(); 
     LocationManager locationManager; 
     locationManager = (LocationManager)getSystemService (Context.LOCATION_SERVICE); 
     Location location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER); 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     GeoPoint find = new GeoPoint((int)(lat*1E6), (int)(lng*1E6)); 
     mv.getController().animateTo(find); 
     mv.invalidate(); 
     break; 
    } 

    return false; 
} 

這是我的方向活動:

public class Direction extends Activity { 

MainActivity maintest; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.direction); 

    // find address 
    final Geocoder geocoder = new Geocoder(maintest); 

    final EditText edMylocation = (EditText) findViewById(R.id.edMylocation); 
    final EditText edEndpoint = (EditText) findViewById(R.id.edEndpoint); 
    Button btDirection = (Button) findViewById(R.id.btDirection); 

    btDirection.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      String endpoint = edEndpoint.getText().toString(); 


      try { 
       List<Address> l = geocoder.getFromLocationName(endpoint, 5); 
       if (l != null && l.size() > 0) { 
        int lat = (int) (l.get(0).getLatitude() * 1000000); 
        int lon = (int) (l.get(0).getLongitude() * 1000000); 
        GeoPoint pt = new GeoPoint(lat, lon); 

        //p = pt; 
        //MapOverlay mar = new MapOverlay(); 
        //listOfOverlays.add(mar); 

        maintest.mv.getController().setZoom(17); 
        //maintest.mv.getController().setCenter(pt); 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       Log.v("Loi: ", "Loi 1 cua tui ne"); 
      } 

     } 
    }); 

} 

}

////////////////////////////////////// 此是功能怎麼辦?(AndylandDev):

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1 && resultCode == RESULT_OK) { 

     String endpoint = data.getStringExtra("endpoint"); 
     final Geocoder geocoder = new Geocoder(MainActivity.this); 
     try { 
      List<Address> l = geocoder.getFromLocationName(endpoint, 5); 
      if (l != null && l.size() > 0) { 
       int lat = (int) (l.get(0).getLatitude() * 1000000); 
       int lon = (int) (l.get(0).getLongitude() * 1000000); 
       GeoPoint pt = new GeoPoint(lat, lon); 

       p = pt; 
       //MapOverlay mar = new MapOverlay(); 
       //listOfOverlays.add(mar); 

       mv.getController().setZoom(17); 
       mv.getController().setCenter(pt); 

      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      Log.v("Loi: ", "Loi 1 cua tui ne"); 
     } 
    } 
} 
+0

什麼是您的類名,它是如何在清單中定義的?根據logcat,它並不真正遵循推薦的命名約定。此外,它聽起來像活動未在清單中正確定義 – codeMagic

回答

1

所以

new Geocoder(maintest); 

很可能拋出一個NullPointerException因爲你maintest變量爲空。一般來說,活動不應該像這樣直接相互訪問。這將是這樣做的正確方法:

當您啓動方向活動:方向活動的

case R.id.direction: 
    //direction 
    Intent direction = new Intent("com.example.zzzzzz.Direction"); 
    startActivityForResult(direction, 1); 
    break; 
中的onCreate

()

... 

btDirection.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     Intent intent = new Intent(); 
     intent.putExtra("endpoint", edEndpoint.getText().toString()); 
     setResult(RESULT_OK, intent); 
     finish(); 
}); 

... 

,然後將此方法添加到MainActivity

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

if (requestCode == 1 && resultCode == RESULT_OK) { 

    String endpoint = data.getStringExtra("endpoint"); 
    //do whatever with it 
} 
+0

非常感謝!我會嘗試! :D –

+0

我得到:11-24 13:26:53.969:E/AndroidRuntime(30137):java.lang.RuntimeException:無法實例化活動ComponentInfo {com.example.zzzzzz/com.example.zzzzzz.MainActivity}:java .lang.NullPointerException now!我發佈我的問題在我的問題的底部!你能幫我看看嗎? HIX!我是初學者 –

+0

你可以發佈整個堆棧跟蹤嗎? –