2014-05-13 114 views
0

我有我的应用程序检测一个人的移动和移动时检查它是否在一定的邻近度,或者至少是我认为它正在做什么。它开始活动,但很快崩溃,我没有logcat输出,因为我的设备在Eclipse上无法识别。我不确定是否应该使用此代码多线程,并且这是问题。当达到接近度时它会发送一个文本,但是这个代码还没有被添加。Android应用程序在活动开始后崩溃

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 [email protected]" + 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(); 
     } 
    } 
+0

越来越捆绑我没有logcat中的S5不被认可的Eclipse,所以我上传通过谷歌驱动器中的文件。 –

+0

您可以使用Play商店中的logcat应用程序。你是否在AndroidManifest.xml中声明了所需的权限? – luxer

+0

是的所有权限都被声明。将尝试应用程序并发布其结果。 –

回答

0

尽量不要将双打值添加到意图,而是使用捆绑并将其附加到意图。

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");