活动开始后Android应用程序崩溃

时间:2014-05-13 00:22:44

标签: android multithreading geolocation proximitysensor

我让我的应用程序检测到一个人移动的时间以及移动时检查它是否处于某个接近状态,或者至少是我认为它正在做什么。它启动活动但很快崩溃,我没有logcat输出,因为我的设备在Eclipse上无法识别。我不确定我是否应该使用多线程代码,这是问题所在。它会在达到接近度时发送文本,但此代码尚未添加。

package com.example.drivetext;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class targetdistance extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target);

    LocationManager locationManager;
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    String provider = LocationManager.GPS_PROVIDER;

    int t = 5000; //milliseconds
    int distance = 5; //meters
    LocationListener myLocationListener = new LocationListener() {
        public void onLocationChanged(Location location){
        //Update application based on new location.
            setProximityAlert();
        }
        public void onProviderDisabled(String provider){
            //Update application if provider disabled.
        }
        public void onProviderEnabled(String provider){
            //Update application if provider enabled.
        }
        public void onStatusChanged(String provider, int status,Bundle extras){
            //Update application if provider hardware status changed.
        }
    };
    locationManager.requestLocationUpdates(provider,t,distance,myLocationListener);
}
    final String TREASURE_PROXIMITY_ALERT = "com.paad.treasurealert";

    private void setProximityAlert() {

        LocationManager locationManager;
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Intent intent = getIntent();
        double coord1 = intent.getParcelableExtra("Coordinate1");
        double coord2 = intent.getParcelableExtra("Coordinate2");
        float seldis = 0;
        seldis = intent.getFloatExtra("SelectedDistance", seldis);

        double lat = coord1;
        double lng = coord2;
        float radius = seldis; //meters
        long expiration = -1; //do not expire

        Intent proxIntent = new Intent(TREASURE_PROXIMITY_ALERT);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1, proxIntent, 0);
        locationManager.addProximityAlert(lat,lng,radius,expiration,proximityIntent);

    }

} 

这是我崩溃的logcat:

05-13 02:13:25.151: E/AndroidRuntime(18946): FATAL EXCEPTION: main
05-13 02:13:25.151: E/AndroidRuntime(18946): Process: com.example.drivetext, PID: 18946
05-13 02:13:25.151: E/AndroidRuntime(18946): java.lang.NullPointerException
05-13 02:13:25.151: E/AndroidRuntime(18946):    at  com.example.drivetext.targetdistance.setProximityAlert(targetdistance.java:49)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at com.example.drivetext.targetdistance.access$0(targetdistance.java:44)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at com.example.drivetext.targetdistance$1.onLocationChanged(targetdistance.java:28)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at android.os.Looper.loop(Looper.java:136)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at android.app.ActivityThread.main(ActivityThread.java:5476)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at java.lang.reflect.Method.invoke(Method.java:515)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
05-13 02:13:25.151: E/AndroidRuntime(18946):    at dalvik.system.NativeStart.main(Native Method)
05-13 02:13:27.121: I/Process(18946): Sending signal. PID: 18946 SIG: 9
05-13 02:13:27.501: D/skia(19753): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 270 
05-13 02:13:27.521: E/MoreInfoHPW_ViewGroup(19753): Parent view is not a TextView

这是我的setdistance类,这是在targetdistance类之前激活的活动。

package com.example.drivetext;

import java.util.ArrayList;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class setdestination extends Activity implements OnMapLongClickListener {

        private GoogleMap map;          
        Location myLocation;
        TextView tvLocInfo;
        double pointfinallat;
        double pointfinallng;
        ListView listview;
        ArrayList<String> distancesendList;
        String finaldistance;
        String contactNo;
        String message;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.setdestination);
            Intent intent = getIntent();
            contactNo = intent.getStringExtra("PhoneNumber");
            message = intent.getStringExtra("TextMessage");
            Toast.makeText(this, contactNo, Toast.LENGTH_LONG).show();
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();                

            ListView distanceList=(ListView)findViewById(R.id.list);


            distancesendList = new ArrayList<String>();
            getdistances();
            // Create The Adapter with passing ArrayList as 3rd parameter
            ArrayAdapter<String> arrayAdapter =      
            new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, distancesendList);
            // Set The Adapter
            distanceList.setAdapter(arrayAdapter); 

            // register onClickListener to handle click events on each item
            distanceList.setOnItemClickListener(new OnItemClickListener()
               {
                        // argument position gives the index of item which is clicked
                       public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
                       {                             
                               String selecteddistance=distancesendList.get(position);
                               finaldistance = selecteddistance;
                               Toast.makeText(getApplicationContext(), "Distance Selected : "+selecteddistance,   Toast.LENGTH_LONG).show();
                            }
               });

            tvLocInfo = (TextView)findViewById(R.id.locinfo);

            FragmentManager myFragmentManager = getFragmentManager();
            MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
            map = myMapFragment.getMap();

            map.setMyLocationEnabled(true);

            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            //myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            //myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            //myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            map.setOnMapLongClickListener(this);

            Button chooseDistance = (Button) findViewById(R.id.btnchooseDistance);
              chooseDistance.setOnClickListener(new View.OnClickListener() {              
                 public void onClick(View arg0) {
                     if (finaldistance != null)
                     {
                         Intent intent3 = new Intent(setdestination.this, targetdistance.class);
                         intent3.putExtra("PhoneNumber", contactNo);
                         intent3.putExtra("TextMessage", message);
                         intent3.putExtra("Coordinate1", pointfinallat);
                         intent3.putExtra("Coordinate2", pointfinallng);
                         intent3.putExtra("SelectedDistance", finaldistance);
                         startActivity(intent3);
                     }
                     }
              });
        }

        void getdistances()
        {
            distancesendList.add("100");
            distancesendList.add("250");
            distancesendList.add("500");
            distancesendList.add("1000");
        }

        public void onMapLongClick(LatLng point) {
            tvLocInfo.setText("New marker added@" + point.toString());
            map.addMarker(new MarkerOptions().position(point).title(point.toString()));

            pointfinallat = point.latitude;
            pointfinallng = point.longitude;

            Toast.makeText(this, point.toString(), Toast.LENGTH_LONG).show();
        }
   }

1 个答案:

答案 0 :(得分:0)

尽量不要将双重值添加到intent中,而是使用Bundle并将其附加到intent。

Bundle b = new Bundle();
b.putDouble("Coordinate1", pointfinallat);
b.putDouble("Coordinate2", pointfinallng);
intent3.putExtras(b);

如果您从活动意图中获取捆绑包

Bundle b = getIntent().getExtras();

double coord1 = b.getDouble("Coordinate1");
double coord2 = b.getDouble("Coordinate2");