これは初めてですが、問題はありません。設定には非同期タスクを開始するためのチェックボックスがあるため、onCreateのifステートメントがあります。タスクが実行される行でJavanullエラーが発生します。
コード
ProgressDialog progressDialog;
double longitude, latitude;
Location location;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;
GPSLocation task = new GPSLocation();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_screen);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean locationCheck = sp.getBoolean("LOCATION", false);
if(locationCheck){
task.execute();
}
else
{
sendEmail();
playSound();
}
}
public double getLatitude()
{
if (location != null)
{
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude()
{
if (location != null)
{
longitude = location.getLongitude();
}
return longitude;
}
public class GPSLocation extends AsyncTask<Void, Void, Void> implements LocationListener
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(ConfirmScreen.this);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
//getgps.cancel(true);
}
});
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
progressDialog.setCancelable(true);
progressDialog.setMessage("Getting GPS Location...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(1);
progressDialog.show();
}
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
sendEmail("GPS: ", Double.toString(longitude).trim(), Double.toString(latitude).trim());
playSound();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendEmail(); //Send Email without co-ords
playSound();
isDataSubmitted = true;
}
}
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@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
}
}
public void backHome(View view)
{
Intent intent = new Intent (this, MainScreen.class);
startActivity(intent);
}
// Method to start playing and looping a sound.
public void playSound()
{
MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean soundCheck = sp.getBoolean("SOUND", false);
if (soundCheck)
{
clickSound.start();
}
}// method end
public void sendEmail()
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{emailValue});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. They didn't include co-ords as they assume you know where they are..\nKind Regards\nDon't Panic! \n\n\n");
try
{ startActivity(Intent.createChooser(i, "Send mail...."));
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
}
}
public void sendEmail(String a, String b, String c)
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{emailValue});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. Please see the co-ords attached and run to their rescue!" +
" If you don't see any co-ords, they didn't check the box and assume you know where they are.\nKind Regards\nDon't Panic! \n\n\n" +
a + b + c);
try
{ startActivity(Intent.createChooser(i, "Send mail...."));
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
return true;
}
}
デバッグ情報
10-03 08:38:16.890: D/dalvikvm(27516): Late-enabling CheckJNI
10-03 08:38:16.990: I/dalvikvm(27516): Turning on JNI app bug workarounds for target SDK version 11...
10-03 08:38:17.030: E/Trace(27516): error opening trace file: No such file or directory (2)
10-03 08:38:17.600: D/dalvikvm(27516): GC_FOR_ALLOC freed 58K, 9% free 12206K/13315K, paused 105ms, total 111ms
10-03 08:38:17.610: I/dalvikvm-heap(27516): Grow heap (frag case) to 13.616MB for 784248-byte allocation
10-03 08:38:17.755: D/dalvikvm(27516): GC_CONCURRENT freed 1K, 8% free 12971K/14087K, paused 35ms+9ms, total 147ms
10-03 08:38:17.940: D/dalvikvm(27516): GC_FOR_ALLOC freed 0K, 8% free 12971K/14087K, paused 32ms, total 32ms
10-03 08:38:17.985: I/dalvikvm-heap(27516): Grow heap (frag case) to 16.606MB for 3136944-byte allocation
10-03 08:38:18.025: D/dalvikvm(27516): GC_CONCURRENT freed 0K, 7% free 16034K/17159K, paused 24ms+1ms, total 40ms
10-03 08:38:18.025: D/dalvikvm(27516): WAIT_FOR_CONCURRENT_GC blocked 4ms
10-03 08:39:44.110: E/SpannableStringBuilder(27516): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-03 08:39:44.110: E/SpannableStringBuilder(27516): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-03 08:39:45.145: E/SpannableStringBuilder(27516): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-03 08:39:45.145: E/SpannableStringBuilder(27516): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-03 08:39:45.910: D/dalvikvm(27516): GC_FOR_ALLOC freed 815K, 11% free 15718K/17543K, paused 9ms, total 9ms
10-03 08:39:45.910: I/dalvikvm-heap(27516): Grow heap (frag case) to 19.016MB for 2850872-byte allocation
10-03 08:39:45.925: D/dalvikvm(27516): GC_CONCURRENT freed 10K, 10% free 18492K/20359K, paused 1ms+1ms, total 12ms
10-03 08:39:45.955: D/dalvikvm(27516): GC_FOR_ALLOC freed 0K, 10% free 18492K/20359K, paused 9ms, total 9ms
10-03 08:39:45.970: I/dalvikvm-heap(27516): Grow heap (frag case) to 29.882MB for 11403440-byte allocation
10-03 08:39:45.990: D/dalvikvm(27516): GC_CONCURRENT freed 0K, 7% free 29628K/31559K, paused 11ms+2ms, total 22ms
10-03 08:39:46.025: D/AndroidRuntime(27516): Shutting down VM
10-03 08:39:46.025: W/dalvikvm(27516): threadid=1: thread exiting with uncaught exception (group=0x41d77300)
10-03 08:39:46.035: E/AndroidRuntime(27516): FATAL EXCEPTION: main
10-03 08:39:46.035: E/AndroidRuntime(27516): java.lang.RuntimeException: Unable to start activity ComponentInfo{cs4084.panic.button/cs4084.panic.button.ConfirmScreen}: java.lang.NullPointerException
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.os.Looper.loop(Looper.java:137)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-03 08:39:46.035: E/AndroidRuntime(27516): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 08:39:46.035: E/AndroidRuntime(27516): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 08:39:46.035: E/AndroidRuntime(27516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-03 08:39:46.035: E/AndroidRuntime(27516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-03 08:39:46.035: E/AndroidRuntime(27516): at dalvik.system.NativeStart.main(Native Method)
10-03 08:39:46.035: E/AndroidRuntime(27516): Caused by: java.lang.NullPointerException
10-03 08:39:46.035: E/AndroidRuntime(27516): at cs4084.panic.button.ConfirmScreen$GPSLocation.onPreExecute(ConfirmScreen.java:89)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.os.AsyncTask.execute(AsyncTask.java:534)
10-03 08:39:46.035: E/AndroidRuntime(27516): at cs4084.panic.button.ConfirmScreen.onCreate(ConfirmScreen.java:46)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.Activity.performCreate(Activity.java:5184)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
10-03 08:39:46.035: E/AndroidRuntime(27516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
10-03 08:39:46.035: E/AndroidRuntime(27516): ... 11 more