2014-05-13 144 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");