2016-04-25 83 views
1

我對java和android studio很新穎。我試圖在用戶位於地圖上指定點的某個距離內時啓動新的活動。嘗試啓動新活動時發生空指針異常

以下課程確定用戶距離每個點的距離,當用戶在一定距離(10米)內時我希望啓動一個新活動,用戶將能夠根據位置回答問題。

然而,當我試着開始新的活動我得到一個錯誤:

E/AndroidRuntime: FATAL EXCEPTION: main 

Process: uk.ac.ucl.cege.cegeg077.ucfapwh.firstapp, PID: 6616 
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106) 
    at uk.ac.ucl.cege.cegeg077.ucfapwh.firstapp.CustomLocationListener.onLocationChanged(CustomLocationListener.java:67) 
    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281) 
    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210) 
    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5343) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

這裏是我到目前爲止的代碼:

import android.app.Activity; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.lang.reflect.Array; 
import java.util.ArrayList; 

public class CustomLocationListener extends Map implements LocationListener { 
public Map parentActivity ; 
public ArrayList<GeoPoint> coordList; 
// this method is called every time the user moves around - i.e. changes  location 
// it pops up a toast message with the new coordinates 
public void onLocationChanged(Location location) { 

    // now measure distance from all the pre-set proximity alerts 
    for (int i = 0; i < coordList.size(); i++) { 

     // create GeoPoint object for each pair of coordinates 
     GeoPoint gp = coordList.get(i); 

     // create Location object 
     Location fixedLoc = new Location("one"); 

     // get lat and lng values from GeoPoint object 
     Float lat = Float.valueOf(String.valueOf(gp.getLatitude())); 
     Float lng = Float.valueOf(String.valueOf(gp.getLongitude())); 

     // set location for proximity alert 
     fixedLoc.setLatitude(lat); 
     fixedLoc.setLongitude(lng); 

     // use Android distanceTo function to calculate the distances 
     float distance = location.distanceTo(fixedLoc); 

     Log.i("****DISTANCE****",String.valueOf(distance)); 
     Log.i("*****FIXEDLOC****", fixedLoc.toString()); 

     for (int j = 0; j < coordList.size(); j++) { 

      if (i == j && distance < 120) { 

       GeoPoint geoPoint = coordList.get(j); 

       Log.i("****PROXI LAT*****", geoPoint.getLatitude().toString()); 
       Log.i("****PROXI LNG****", geoPoint.getLongitude().toString()); 

       Intent quizIntent = new Intent(getApplicationContext(),Questions.class); 
       startActivity(quizIntent); 

      } 
     } 

    } 

} 

// these methods are called when the GPS is switched on or off 
// and will allow the App to warn the user and then 
// shut down without an error 
public void onProviderDisabled(String s) { 
} 
public void onProviderEnabled(String s) { 
} 

// this method is required by the LocationListener 
// we do not need to do anything here 
// but in a full implementation this could be used to react when the GPS signal is not available 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 

} 

道歉窮人格式。任何幫助將不勝感激。

回答

2

解決:

private Context context; 

public NonActivityClass(Context context) { 
    this.context = context.getApplicationContext(); 
} 

然後火的意圖,並啓動新的活動:

   Intent i = new Intent(context, Activity.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(i); 

要從非活動類需要下面的代碼檢索上下文

感謝您的幫助。顯示java.lang.NullPointerException:試圖調用虛擬方法java.lang.String中android.content.Context

get Context in non-Activity class

0

您不能使用應用程序上下文來啓動應用程序。你應該使用活動上下文。如果(猜測)地圖是那麼你的活動更新開始活動代碼

Intent quizIntent = new Intent(this,Questions.class); 
       startActivity(quizIntent); 

詳情爲什麼應用程序上下文不能使用啓動活動https://possiblemobile.com/2013/06/context/

+0

我做你的建議得到了下面的錯誤。getPackageName()'的空對象引用 – pwhc

0

從我的角度來看,你有兩個選擇,以獲得它:

  1. CustomLocationListener聲明爲private,例如NameOfYourActivity

接下來,將Intent構造函數從getApplicationContext()更改爲NameOfYourActivity.this

  • 設置NameOfYourActivity作爲CustomLocationListener屬性的Context,然後從getApplicationContext()Intent構造改變到上下文屬性。
  • 希望它可以幫助

    0

    你得到空,因爲你的類沒有擴展的Android活動。您可以像解釋here一樣獲取非活動類中的應用程序上下文。

    +0

    我使用了下面的代碼,即時通訊不知道如果我做的是正確的:'public Context context; public CustomLocationListener(Context context){this.context = context; }'。然後我用下面的方法聲明瞭一個新實例:'Intent quizIntent = new Intent(new CustomLocationListener(this),Questions.class)''。然而,我得到了同樣的結果** java.lang.NullPointerException:試圖調用虛擬方法java.lang.String android.content.Context.getPackageName()對一個空對象**錯誤 – pwhc

    相關問題