0
votes

Here is my code for MainActivity.java

package com.example.com.draft1;

import com.google.android.maps.GeoPoint;
 import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.view.KeyEvent;
import com.google.android.maps.MapController;



public class MainActivity extends MapActivity {



MapView mapView;
MapController mc;
GeoPoint p;

String coordinates[]={"1.352566007","103.78921587"};
double lat=Double.parseDouble(coordinates[0]);
double lng=Double.parseDouble(coordinates[1]);

@Override
public void onCreate(Bundle savedInstanceState) {


    p=new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
    mapView=(MapView)findViewById(R.id.mapView);


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mc.setCenter(p);

    //mc.setZoom(13);
    //mapView.invalidate();


    //myMapController.setCenter(new GeoPoint((int)(lat*1E6),(int)(lng*1E6)));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
@Override
protected boolean isRouteDisplayed() 

{
    //adssad

    return false;

}



}

Now , the above code leads to a force close .

Here are the LogCat details for the above : 09-23 16:37:38.494: D/dalvikvm(18710): GC_CONCURRENT freed 244K, 41% free 3354K/5639K, external 0K/0K, paused 3ms+4ms 09-23 16:37:38.764: D/dalvikvm(18710): GC_CONCURRENT freed 530K, 44% free 3378K/5959K, external 0K/0K, paused 2ms+7ms 09-23 16:37:38.894: D/dalvikvm(18710): GC_CONCURRENT freed 376K, 42% free 3513K/5959K, external 0K/0K, paused 3ms+4ms 09-23 16:37:39.014: D/dalvikvm(18710): GC_CONCURRENT freed 510K, 43% free 3523K/6087K, external 0K/0K, paused 3ms+4ms 09-23 16:37:39.134: D/dalvikvm(18710): GC_CONCURRENT freed 334K, 40% free 3700K/6087K, external 0K/0K, paused 3ms+4ms 09-23 16:37:39.194: I/HtcTextSelection(18710): (sendOnTextChanged) stopTextSelectionMode 09-23 16:37:39.244: D/dalvikvm(18710): GC_EXTERNAL_ALLOC freed 698K, 47% free 3319K/6151K, external 0K/0K, paused 45ms 09-23 16:37:39.264: D/AndroidRuntime(18710): Shutting down VM 09-23 16:37:39.264: W/dalvikvm(18710): threadid=1: thread exiting with uncaught exception (group=0x400dc5a0) 09-23 16:37:39.264: E/AndroidRuntime(18710): FATAL EXCEPTION: main 09-23 16:37:39.264: E/AndroidRuntime(18710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.draft1/com.example.com.draft1.MainActivity}: java.lang.NullPointerException 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.ActivityThread.access$1500(ActivityThread.java:135) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.os.Handler.dispatchMessage(Handler.java:99) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.os.Looper.loop(Looper.java:150) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.ActivityThread.main(ActivityThread.java:4389) 09-23 16:37:39.264: E/AndroidRuntime(18710): at java.lang.reflect.Method.invokeNative(Native Method) 09-23 16:37:39.264: E/AndroidRuntime(18710): at java.lang.reflect.Method.invoke(Method.java:507) 09-23 16:37:39.264: E/AndroidRuntime(18710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 09-23 16:37:39.264: E/AndroidRuntime(18710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 09-23 16:37:39.264: E/AndroidRuntime(18710): at dalvik.system.NativeStart.main(Native Method) 09-23 16:37:39.264: E/AndroidRuntime(18710): Caused by: java.lang.NullPointerException 09-23 16:37:39.264: E/AndroidRuntime(18710): at com.example.com.draft1.MainActivity.onCreate(MainActivity.java:40) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 09-23 16:37:39.264: E/AndroidRuntime(18710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 09-23 16:37:39.264: E/AndroidRuntime(18710): ... 11 more

So when i comment the .setCenter() statement , the app works perfectly.However , on uncommenting , it force closes

Any clues , as to why this is happening ?

Appreciate all your help

1

1 Answers

0
votes

Extremely stupid error. Hadn't initialized mc ( MapController )

my bad