开发者

App crashes when it reaches myMapView.getController()

开发者 https://www.devze.com 2023-03-10 14:25 出处:网络
For some reason this VERY simple program crashes when it reaches the getController() method. Its almost like the debugger is trying to tell me something, but its all garble.Believe it or not there isn

For some reason this VERY simple program crashes when it reaches the getController() method. Its almost like the debugger is trying to tell me something, but its all garble.Believe it or not there isn't a clear explanation or exception given, ha. But it definitely blows up on this line. If I take it out of the code the app will run a basic map. I have tried everything imaginable and I can't figure out the problem. Does anyone have any thoughts? Thanks in advance.

public class Main extends MapActivity {

MapView myMapView;
MapController mc;
GeoPoint point;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    double lon = -79.9;
    double lat = 40.4;

    myMapView = (MapView) findViewById(R.layout.main);

    point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

    mc = myMapView.getController();
    mc.animateTo(point);
    mc.setZoom(13);

}
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

And the main.xml code:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.google.android.maps.MapView
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:enabled="true" 
        android:clickable="true"
        android:apiKey="0SN6PntBTxgMIeekPQm2Of1gnlDiHmrTm8GG2xQ"
        ></com.google.android.maps.MapView>     
         </LinearLayout>

And Finally, the manifest.xml code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.simplyDesign.BarCraw" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".Main" android:label="@string/app_name">
            <intent-filter>
                <action and开发者_如何学运维roid:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
</manifest>


You should give your MapView an android:id and feed it to findViewById(). In your code you search for a view with the Id of your whole layout. This can only return null... and give you a NullPointerException when invoking getController() on null.


define id for MapView in xml

<com.google.android.maps.MapView
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:enabled="true" 
        android:clickable="true"
        android:apiKey="0SN6PntBTxgMIeekPQm2Of1gnlDiHmrTm8GG2xQ"
         android:id="@+id/mapView"
        ></com.google.android.maps.MapView> 

and change this line..

myMapView = (MapView) findViewById(R.layout.mapView);
0

精彩评论

暂无评论...
验证码 换一张
取 消