简体   繁体   中英

Android MapController crashing my app

I'm creating a mapView activity and for some reason this code isn't working:

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

This line specifically is causing the app to crash:

MapController mapController = mapView.getController();

When I take that line out, the app works like it should, but when I put it in, it crashes when I start the activity, with the error message:

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

Here is my call to the 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);

                }
            });
    }
}

}

Can anyone give me some direction as to what I'm doing wrong?

The problem is in this line

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

You are Linding Layout mapView wont get initialize try to change with ID ie

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

Can you try replacing this line:

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

with this one:

 MapView mapView = (MapView)this.findViewById(R.id.<your mapView Id>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM