次のコードでデバッグ/回避しようとしているヌル ポインター例外に問題があります。waypointCursor を null に初期化しようとしていますが、コードの後半で null と等しいかどうかを確認する必要があります。これを行うと、NullPointerException が発生します。それを回避するために助けが必要です。以下にコードと詳細を示します。
メソッドを呼び出すコードは次のとおりです。
private void displayCursor(Cursor tracksCursor)
{
UnitsI18n mUnits;
mUnits = new UnitsI18n( this, new UnitsI18n.UnitsChangeListener()
{
@Override
public void onUnitsChange()
{
//Do Nothing
}
} );
Log.i(TAG, "TRACKS CONTENT URI:"+Tracks.CONTENT_URI);
StatisticsCalculator stats = new StatisticsCalculator(this, mUnits, this);
//UNCOMMENT THIS ONCE I FIGURE OUT WHAT IS GOING ON
if (Tracks.CONTENT_URI != null) {
stats.updateCalculations(Tracks.CONTENT_URI);
//stats.updateCalculations(Uri.withAppendedPath(Tracks.CONTENT_URI, TRACKURI));
}
NullPointerException が発生している StatisticsCalculator のコードを次に示します。
public void updateCalculations( Uri trackUri )
{
mStarttime = -1;
mEndtime = -1;
mMaxSpeed = 0;
mAverageActiveSpeed = 0;
mMaxAltitude = 0;
mMinAltitude = 0;
mAscension = 0;
mDistanceTraveled = 0f;
mDuration = 0;
long duration = 1;
double ascension = 0;
ContentResolver resolver = mContext.getContentResolver();
Cursor waypointsCursor = null;
try
{
waypointsCursor = resolver.query(
Uri.withAppendedPath( trackUri, "waypoints" ),
new String[] { "max (" + Waypoints.TABLE + "." + Waypoints.SPEED + ")"
, "max (" + Waypoints.TABLE + "." + Waypoints.ALTITUDE + ")"
, "min (" + Waypoints.TABLE + "." + Waypoints.ALTITUDE + ")"
, "count(" + Waypoints.TABLE + "." + Waypoints._ID + ")" },
null, null, null );
if( waypointsCursor.moveToLast() )
{
mMaxSpeed = waypointsCursor.getDouble( 0 );
mMaxAltitude = waypointsCursor.getDouble( 1 );
mMinAltitude = waypointsCursor.getDouble( 2 );
long nrWaypoints = waypointsCursor.getLong( 3 );
waypointsText = nrWaypoints + "";
}
waypointsCursor.close();
waypointsCursor = resolver.query(
Uri.withAppendedPath( trackUri, "waypoints" ),
new String[] { "avg (" + Waypoints.TABLE + "." + Waypoints.SPEED + ")" },
Waypoints.TABLE + "." + Waypoints.SPEED +" > ?",
new String[] { ""+Constants.MIN_STATISTICS_SPEED },
null );
if( waypointsCursor.moveToLast() )
{
mAverageActiveSpeed = waypointsCursor.getDouble( 0 );
}
}
finally
{
if( waypointsCursor != null )
{
waypointsCursor.close();
}
}
StatisticsCalculator クラスのコード行 (75 行目) は、次のコード行です。
if( waypointsCursor.moveToLast() )
次の行で waypointsCursor が null に設定されるのは好ましくありません。
Cursor waypointsCursor = null;
上記の try ステートメントのすぐ上。ただし、この行で waypointsCursor が null と等しいかどうかを確認できるように、null に設定する必要があります。
logcat の出力は次のとおりです。
04-12 18:29:16.795: I/OGT.TrackList(6115): TRACKS CONTENT URI:content://com.android.gpstracker/tracks
04-12 18:29:16.795: E/OGT.GPStrackingProvider(6115): Unable to come to an action in the query uri: content://com.android.gpstracker/tracks/waypoints
04-12 18:29:16.795: D/AndroidRuntime(6115): Shutting down VM
04-12 18:29:16.807: W/dalvikvm(6115): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-12 18:29:16.915: E/AndroidRuntime(6115): FATAL EXCEPTION: main
04-12 18:29:16.915: E/AndroidRuntime(6115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gpstracker/com.polaris.epicriders.Rides.RidesActivity}: java.lang.NullPointerException
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.os.Looper.loop(Looper.java:137)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-12 18:29:16.915: E/AndroidRuntime(6115): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 18:29:16.915: E/AndroidRuntime(6115): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-12 18:29:16.915: E/AndroidRuntime(6115): at dalvik.system.NativeStart.main(Native Method)
04-12 18:29:16.915: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.android.gpstracker.actions.utils.StatisticsCalculator.updateCalculations(StatisticsCalculator.java:75)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.displayCursor(RidesActivity.java:901)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.displayIntent(RidesActivity.java:880)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.access$6(RidesActivity.java:832)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity$8.onCheckedChanged(RidesActivity.java:198)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.RadioGroup.access$600(RadioGroup.java:54)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.widget.CompoundButton.setChecked(CompoundButton.java:129)
04-12 18:29:16.915: E/AndroidRuntime(6115): at com.polaris.epicriders.Rides.RidesActivity.onCreate(RidesActivity.java:222)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.Activity.performCreate(Activity.java:5104)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-12 18:29:16.915: E/AndroidRuntime(6115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
どんな助けでも大歓迎です。