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 8356751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:16:40+00:00 2026-06-09T10:16:40+00:00

This is a location based app.The MainActivity.java is the first file to get executed,But

  • 0

This is a location based app.The MainActivity.java is the first file to get executed,But the app Force Closes as soon as it is opened.LogCat included.
Please help.Hope to get the answer here.
MainActivity.java

package com.location.sensor;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
     LocationListener locationlistener;
     Location loc;
     PendingIntent pendint;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
     locationlistener=new MyLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistener);
            Button btn=(Button) findViewById(R.id.btn_OK);
            //Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
            //btn.startAnimation(anim);
            TextView n= (TextView)findViewById(R.id.tv);
                btn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
                        anim.setDuration(3000);
                        //v.startAnimation(anim);
                        TextView n= (TextView)findViewById(R.id.tv);
                        EditText txt=(EditText) findViewById(R.id.txt_username);
                        //Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
                        n.startAnimation(anim);
                        String s=txt.getText().toString();
                        n.setText(s);
                        n.setBackgroundResource(R.drawable.icn);                    
                    }
                });

                EditText txt=(EditText) findViewById(R.id.txt_username);
                //Animation anim= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.jump);
                String s=txt.getText().toString();
                    //LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    //LocationListener locationlistener=new MyLocationListener();
                    //lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 10, locationlistener);
                    Intent intent=new Intent(this,NotificationClass.class);
                    Bundle extras=new Bundle();
                    extras.putString("alarm", s);
                    intent.putExtras(extras);
                    PendingIntent.getActivity(this, 0, intent, 0);
                    lm.addProximityAlert(loc.getLatitude(), loc.getLongitude(), 8, -1, pendint);


    }

 private class MyLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
                loc.set(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "Please enable GPS in Settings!", Toast.LENGTH_LONG).show();
            Message msg = handler.obtainMessage();
             msg.arg1 = 1;
             handler.sendMessage(msg);
        }



        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub


        }

 }
private final Handler handler = new Handler() {
         @Override
        public void handleMessage(Message msg) {
              if(msg.arg1 == 1){
                   if (!isFinishing()) { // Without this in certain cases application will show ANR
                        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                        builder.setMessage("Your GPS is disabled! Would you like to enable it?").setCancelable(false).setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
                             @Override
                            public void onClick(DialogInterface dialog, int id) {
                                  Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                  startActivity(gpsOptionsIntent);
                              }
                         });
                         builder.setNegativeButton("Do nothing", new DialogInterface.OnClickListener() {
                              @Override
                            public void onClick(DialogInterface dialog, int id) {
                                   dialog.cancel();
                              }
                         });
                         AlertDialog alert = builder.create();
                         alert.show();
                   }
               }

           }
    };
 }

NotificationClass.java

package com.location.sensor;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class NotificationClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView txtview=new TextView(this);
        txtview.setText("Hey,Im Sorry");
        setContentView(txtview);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        String data=getIntent().getExtras().getString("alarm").toString();
        Bundle enter= getIntent().getExtras();
        if(enter.getBoolean("KEY_PROXIMITY_ENTERING"))
        {
            setContentView(R.layout.notif);
        TextView tv=(TextView) findViewById(R.id.txt_pendint);
        Button btn=(Button) findViewById(R.id.btn_cancel);
        tv.setText(data);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(0);

            }
        });
        }
    }
@Override
protected Dialog onCreateDialog(int id)
{
    switch(id)
    {
    case 0:

        return new AlertDialog.Builder(this)
                .setIcon(R.drawable.excl_mark)
                .setTitle("Do you want to Cancel?")
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        finish();
                    }
                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(), "Cancel Clicked!", Toast.LENGTH_LONG).show();
                    }
                })
                .create();
    }
    return null;

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.location.sensor"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:icon="@drawable/gps1"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".NotificationClass"
            android:label="YOU ARE IN PROXIMITY AREA!!!"
            android:exported="false">
            <intent-filter>
                <action android:name="com.locationsensor.notif"/>
                <category android:name="android.intent.category.DEFAULT"/> 
            </intent-filter>
        </activity>
    </application>

</manifest>

LogCat

    08-01 17:51:17.546: D/AndroidRuntime(483): Shutting down VM
08-01 17:51:17.546: W/dalvikvm(483): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-01 17:51:17.566: E/AndroidRuntime(483): FATAL EXCEPTION: main
08-01 17:51:17.566: E/AndroidRuntime(483): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.location.sensor/com.location.sensor.MainActivity}: java.lang.NullPointerException
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.os.Looper.loop(Looper.java:123)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 17:51:17.566: E/AndroidRuntime(483):  at java.lang.reflect.Method.invokeNative(Native Method)
08-01 17:51:17.566: E/AndroidRuntime(483):  at java.lang.reflect.Method.invoke(Method.java:507)
08-01 17:51:17.566: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 17:51:17.566: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 17:51:17.566: E/AndroidRuntime(483):  at dalvik.system.NativeStart.main(Native Method)
08-01 17:51:17.566: E/AndroidRuntime(483): Caused by: java.lang.NullPointerException
08-01 17:51:17.566: E/AndroidRuntime(483):  at com.location.sensor.MainActivity.onCreate(MainActivity.java:68)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 17:51:17.566: E/AndroidRuntime(483):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-01 17:51:17.566: E/AndroidRuntime(483):  ... 11 more
08-01 17:56:17.656: I/Process(483): Sending signal. PID: 483 SIG: 9
  • 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-09T10:16:41+00:00Added an answer on June 9, 2026 at 10:16 am

    It looks like your ‘loc’ variable is NOT instantiated, which is why you are getting a ‘NULL Pointer” exception (which will cause a FC of your app).

    These 2 lines in your stack trace point me to that:

    Caused by: java.lang.NullPointerException
    08-01 17:51:17.566: E/AndroidRuntime(483): at com.location.sensor.MainActivity.onCreate(MainActivity.java:68)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a location based app using google places, but repeatedly get
I am writing a location based App and want to get some wisdom on
Created a Location alarm based app. Everything is working fine on emulator. But the
I am working on an Location Based Application. In this application I am using
I have a requirement in a shell script. I get this location information from
I'm using the Google API to get a user's location on this site .
I am developing an Augmented Reality app based on location. It is working perfectly
I am doing an app based on mapview. I am getting the location(place) and
What is the appropriate way to implement a location-based Android app that periodically sends
I'm currently building my first Django-based app, which is proceeding fairly well, thus far.

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.