0

我正在创建一个 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();

当我取出那条线时,应用程序会正常工作,但是当我放入它时,它会在我启动 Activity 时崩溃,并显示错误消息:

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

                }
            });
    }
}

}

谁能给我一些关于我做错了什么的方向?

4

2 回答 2

2

问题出在这一行

 MapView mapView = (MapView)this.findViewById(R.layout.map_view);

你是 Linding LayoutmapView不会得到初始化尝试用 ID 改变,即

 MapView mapView = (MapView)this.findViewById(R.id.map_view);
于 2012-07-06T04:41:17.883 回答
2

您可以尝试替换此行吗:

 MapView mapView = (MapView)this.findViewById(R.layout.map_view);

有了这个:

 MapView mapView = (MapView)this.findViewById(R.id.<your mapView Id>);
于 2012-07-06T04:42:07.660 回答