Android App FCs自启动时。LogCat和其他随附文件

Android App FCs自启动时。LogCat和其他随附文件,android,geolocation,nullpointerexception,android-manifest,android-activity,Android,Geolocation,Nullpointerexception,Android Manifest,Android Activity,这是一个基于位置的应用程序。MainActivity.java是第一个执行的文件,但应用程序强制在打开后立即关闭。包括LogCat。 请帮忙。希望在这里得到答案。 MainActivity.java package com.location.sensor; import android.app.Activity; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.Co

这是一个基于位置的应用程序。MainActivity.java是第一个执行的文件,但应用程序强制在打开后立即关闭。包括LogCat。
请帮忙。希望在这里得到答案。
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();
                   }
               }

           }
    };
 }
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;

}
}
<?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>
NotificationClass.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();
                   }
               }

           }
    };
 }
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;

}
}
<?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>
AndroidManifest.xml

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

           }
    };
 }
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;

}
}
<?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>

看起来你的'loc'变量没有实例化,这就是为什么你会得到一个'NULL Pointer'异常(这将导致你的应用出现FC)

堆栈跟踪中的这两行代码向我指出:

原因:java.lang.NullPointerException
08-01 17:51:17.566:E/AndroidRuntime(483):在com.location.sensor.MainActivity.onCreate(MainActivity.java:68)

您不能在
btn.setOnClickListener()中调用
findViewById()
。您正在调用
findViewById()
在您的
OnClickListener
类的匿名内部类中。因此,Android将在您的
OnClickListener
中查找方法
findViewById()

只需在
onCreate()中初始化
视图

或者您可以创建一个名为
private Context Context
的变量,然后在
onCreate()
中使其成为
this

protected void onCreate(Bundle savedInstanceState) {
     context = this;
     //... your code
}

并在
btn.setOnClickListener()中调用您的
context.findViewById()
,在您的主要活动结束时,有一些内容被破坏。onCreate():

我假设您希望将PendingEvent传递给
AddProximityErt()
。但是,您不需要处理从
PendingEvent.getActivity()
返回的PendingEvent。然后将
pendint
传递给
AddProximityErt())
而且肯定是null。您可能希望执行以下操作:

pendint = PendingIntent.getActivity(this, 0, intent, 0);
lm.addProximityAlert(loc.getLatitude(), loc.getLongitude(), 8, -1, pendint);
另外,对变量
loc
调用
getLatitude()
getLongitude()
,但该变量也从未设置为任何值,因此它肯定是null


修复这些问题,看看你能做多少。

我正在做类似的事情。当我只使用onLocationChanged时,代码工作得很好。当我使用onLocationChanged和addProximityAlert时,问题开始了

添加接近警报后,onLocationChanged从未被调用,onLocationChanged方法中的初始化对象开始给我一个空指针


关于这一点我不太确定,但我可能还想提出一个问题,即随addProximityAlert一起调用onLocationChanged是否有问题?或者我们需要做一些额外的事情来让它们一起工作?

您是否使用了调试器并逐步执行了on create方法d?有很多可能出现故障的地方。一个是按钮代码,在layout_main.xml文件中是否有一个名为btn_OK的按钮?使用调试器查看什么是空的。是的,我有一个“btn_OK”。在我在“Activity”类中创建“private”变量之前,它工作正常。MainActivity的第68行是什么?仍然不工作。i instanti在onCreate()方法的外部和内部使用null初始化loc。它仍然不起作用。它会起作用,但可能不是您遇到的唯一问题。您的错误似乎是
位置
对象。好的。但我已在MyLocationListener()方法的onLocationUpdate()中初始化了loc。
onLocationChanged()在您调用
requestLocationUpdates()
和尝试使用它之间,将不会调用
。事实上,不能保证会调用
onLocationChanged()
。您需要了解,请求位置更新不会同步导致“onLocationChanged”()待调用。它只是要求您在将来收到位置更改的通知。哦,好的。谢谢:)我将相应地更改代码并在此处更新。不,问题出在Activity类中的location对象上。当我尝试访问它时,它仍然为空。最好是直接从onLocationChanged获得位置传递,并将其传递给m我想做你想做的手术,对我有用。