Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:38:28+00:00 2026-06-14T21:38:28+00:00

I am doing a Google map application. I wrote a function to find address.

  • 0

I am doing a Google map application.
I wrote a function to find address.
When I wrote code at main activity, it’s work.
But when I wrote at another activity. It’s always force close app.

What is wrong and how I can fix it?

This is my log:

11-24 08:56:52.659: E/AndroidRuntime(16334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zzzzzz/com.example.zzzzzz.Direction}: java.lang.NullPointerException

This is my mainactivity code find address:

        // find address
    final Geocoder geocoder = new Geocoder(MainActivity.this);

    final EditText ed = (EditText) findViewById(R.id.edAddress);
    Button btAddress = (Button) findViewById(R.id.btAddress);
    btAddress.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            String Ten = ed.getText().toString();
            try {
                List<Address> l = geocoder.getFromLocationName(Ten, 5);
                if (l != null && l.size() > 0) {
                    int lat = (int) (l.get(0).getLatitude() * 1000000);
                    int lon = (int) (l.get(0).getLongitude() * 1000000);
                    GeoPoint pt = new GeoPoint(lat, lon);

                    p = pt;
                    MapOverlay mar = new MapOverlay();
                    listOfOverlays.add(mar);

                    mv.getController().setZoom(17);
                    mv.getController().setCenter(pt);

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.v("Loi: ", "Loi 1 cua tui ne");
            }
        }
    });

and this is my function start other activity:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.about:
        //about
        Intent about = new Intent("com.example.zzzzzz.About");
        startActivity(about);
        break;
    case R.id.setting:
        //setting
        Intent setting = new Intent("com.example.zzzzzz.Setting");
        startActivity(setting);
        break;
    case R.id.direction:
        //direction
        Intent direction = new Intent("com.example.zzzzzz.Direction");
        startActivity(direction);
        break;
    case R.id.MyLocation:
        //mylocation
        //Intent mylocation = new Intent("com.example.zzzzzz.MyLocation");
        //startActivity(mylocation);
        initLocationManagerV2();
        LocationManager locationManager;
        locationManager = (LocationManager)getSystemService (Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        GeoPoint find = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
        mv.getController().animateTo(find);
        mv.invalidate();
        break;
    }

    return false;
}

this is my Direction activity:

public class Direction extends Activity {

MainActivity maintest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.direction);

    // find address
    final Geocoder geocoder = new Geocoder(maintest);

    final EditText edMylocation = (EditText) findViewById(R.id.edMylocation);
    final EditText edEndpoint = (EditText) findViewById(R.id.edEndpoint);
    Button btDirection = (Button) findViewById(R.id.btDirection);

    btDirection.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String endpoint = edEndpoint.getText().toString();


            try {
                List<Address> l = geocoder.getFromLocationName(endpoint, 5);
                if (l != null && l.size() > 0) {
                    int lat = (int) (l.get(0).getLatitude() * 1000000);
                    int lon = (int) (l.get(0).getLongitude() * 1000000);
                    GeoPoint pt = new GeoPoint(lat, lon);

                    //p = pt;
                    //MapOverlay mar = new MapOverlay();
                    //listOfOverlays.add(mar);

                    maintest.mv.getController().setZoom(17);
                    //maintest.mv.getController().setCenter(pt);

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.v("Loi: ", "Loi 1 cua tui ne");
            }

        }
    });

}

}

//////////////////////////////////////
this is function i do (AndylandDev):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {

        String endpoint = data.getStringExtra("endpoint");
        final Geocoder geocoder = new Geocoder(MainActivity.this);
        try {
            List<Address> l = geocoder.getFromLocationName(endpoint, 5);
            if (l != null && l.size() > 0) {
                int lat = (int) (l.get(0).getLatitude() * 1000000);
                int lon = (int) (l.get(0).getLongitude() * 1000000);
                GeoPoint pt = new GeoPoint(lat, lon);

                p = pt;
                //MapOverlay mar = new MapOverlay();
                //listOfOverlays.add(mar);

                mv.getController().setZoom(17);
                mv.getController().setCenter(pt);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v("Loi: ", "Loi 1 cua tui ne");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:38:29+00:00Added an answer on June 14, 2026 at 9:38 pm

    So

    new Geocoder(maintest);
    

    is likely throwing a NullPointerException because you’re maintest variable is null. In general, activities should never have direct access to each other like this. This would be the proper way to do it:

    When you start the Direction Activity:

    case R.id.direction:
        //direction
        Intent direction = new Intent("com.example.zzzzzz.Direction");
        startActivityForResult(direction, 1);
        break;
    

    in onCreate() of Direction Activity

    ...
    
    btDirection.setOnClickListener(new OnClickListener() {
    
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("endpoint", edEndpoint.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
    });
    
    ...
    

    And then add this method to MainActivity

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    if (requestCode == 1 && resultCode == RESULT_OK) {
    
        String endpoint = data.getStringExtra("endpoint");
        //do whatever with it
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am trying to dispaly a basic google map on my application but that's not
I'm am curently doing an application with Google Maps, where I am checking the
I want to use google map API for my desktop application. The application will
I'm working on an application that generates a large number of Google Map markers
I am currently doing a tracking application and am using Google maps fusion table
In my Google Maps application I can place markers on the map, and I
I'm back with more work on the Google Map that I've been working on.
I'm writing a Google Maps application, for the most part it works fine. But
I'm doing an application in Django that has a small map in the user
I am doing a c#.net application in which i want to show a map(poly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.