我正在创建一个mapView活动,由于某些原因,此代码无效:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.map_view);
MapView mapView = (MapView)this.findViewById(R.layout.map_view);
MapController mapController = mapView.getController();
这一行特别导致应用程序崩溃:
MapController mapController = mapView.getController();
当我拿出那条线时,应用程序就像它应该的那样工作,但当我把它放入时,它会在我开始活动时崩溃,并显示错误消息:
07-05 21:27:24.857: E/AndroidRuntime(23801): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uie.top25.seattle/com.uie.top25.seattle.MapDetailActivity}: java.lang.NullPointerException
这是我对mapView的调用:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.detail);
Bundle bundle = this.getIntent().getExtras();
ListData listdata = bundle.getParcelable("uie.top25.seattle.listdata");
// int itemId = this.getIntent().getIntExtra(DiningActivity.ITEM_ID, -1);
String itemTitle = listdata.getTitle();
int itemNum = listdata.getItem();
String sHead = listdata.getSubhead();
String dText = listdata.getDetailText();
final float latitude = listdata.getLatitude();
final float longitude = listdata.getLongitude();
Log.i(TAG, "Title that is set : " + itemTitle);
Log.i(TAG, "Item Number that is set : " + itemNum);
Log.i(TAG, "Latitude that is set : " + latitude);
Log.i(TAG, "Longitude that is set : " + longitude);
if (itemNum >= 0)
{
TextView tView = (TextView) this.findViewById(R.id.textTitle);
tView.setText(itemTitle);
TextView nView = (TextView) this.findViewById(R.id.itemNumber);
nView.setText("" + itemNum);
TextView subHead = (TextView) this.findViewById(R.id.textSubheader);
subHead.setText(sHead);
TextView details = (TextView) this.findViewById(R.id.itemDescription);
details.setText(dText);
ImageButton mapButton = (ImageButton) this.findViewById(R.id.mapButton);
mapButton.setOnClickListener(new View.OnClickListener() {
//this is where I call my mapView from
@Override
public void onClick(View v)
{
Intent mapIntent = new Intent(DetailActivity.this, MapDetailActivity.class);
//mapIntent.putExtra("uie.top25.seattle.listlat", latitude);
//mapIntent.putExtra("uie.top25.seattle.longitude", longitude);
startActivity(mapIntent);
}
});
}
}
}
任何人都可以给我一些关于我做错的方向吗?
答案 0 :(得分:2)
问题出在这一行
MapView mapView = (MapView)this.findViewById(R.layout.map_view);
你是Linding Layout mapView
不会初始化尝试改变ID,即
MapView mapView = (MapView)this.findViewById(R.id.map_view);
答案 1 :(得分:2)
您可以尝试更换此行:
MapView mapView = (MapView)this.findViewById(R.layout.map_view);
这一个:
MapView mapView = (MapView)this.findViewById(R.id.<your mapView Id>);