我在做一個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");
}
}
}
什麼是您的類名,它是如何在清單中定義的?根據logcat,它並不真正遵循推薦的命名約定。此外,它聽起來像活動未在清單中正確定義 – codeMagic