footcount.java
activity_footcount.xml
<Button
android:id="@+id/button2"
android:onClick="onTestClick"
android:layout_width="150dip"
android:layout_height="40dip"
android:layout_marginLeft="90dip"
android:layout_marginTop="20dip"
android:text="开始测试"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:background="@drawable/shape" />
点击这个按钮后,app就停止服务了。然后报错。
logcat
01-13 19:01:05.041: E/MediaPlayer-JNI(5705): QCMediaPlayer mediaplayer NOT present
01-13 19:01:13.151: E/AndroidRuntime(5705): FATAL EXCEPTION: main
01-13 19:01:13.151: E/AndroidRuntime(5705): Process: com.example.sport, PID: 5705
01-13 19:01:13.151: E/AndroidRuntime(5705): java.lang.IllegalStateException: Could not execute method of the activity
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View$1.onClick(View.java:4028)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View.performClick(View.java:4788)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View$PerformClick.run(View.java:19911)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.os.Handler.handleCallback(Handler.java:739)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.os.Handler.dispatchMessage(Handler.java:95)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.os.Looper.loop(Looper.java:135)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.app.ActivityThread.main(ActivityThread.java:5269)
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Native Method)
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Method.java:372)
01-13 19:01:13.151: E/AndroidRuntime(5705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
01-13 19:01:13.151: E/AndroidRuntime(5705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
01-13 19:01:13.151: E/AndroidRuntime(5705): Caused by: java.lang.reflect.InvocationTargetException
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Native Method)
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Method.java:372)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View$1.onClick(View.java:4023)
01-13 19:01:13.151: E/AndroidRuntime(5705): ... 10 more
01-13 19:01:13.151: E/AndroidRuntime(5705): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
01-13 19:01:13.151: E/AndroidRuntime(5705): at com.example.sport.FootCount.onTestClick(FootCount.java:252)
01-13 19:01:13.151: E/AndroidRuntime(5705): ... 13 more
我想问下,Caused by: java.lang.reflect.InvocationTargetException这个错误是什么意思啊?
还有Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference这个为什么是空指针?
public class FootCount extends Activity{
TextView thistimecount;
TextView havecount;
Button button_test = null;
Button button_start = null;
DBAdapter mHelper;
SQLiteDatabase mDb;
Cursor mCursor;
SimpleCursorAdapter mAdapter;
boolean choose_s_t = true; //true表示当前是startcount状态
// 定义LocationManager对象
LocationManager locationManager;
//Location currentLocation;
// 接受位置更新的监听器
double have_pasted = 0; //累计走过的路程
double seconds_pasted = 0; //2s内走过的路程
double current_Longitude = 0; //开始(当前)获取的经度
double current_Latitude = 0; //开始(当前)获取的纬度
double percountleg = 0; //一步经过的距离(应该存入数据库中)
int foot_count = 0; //累计走过的步数(从数据库中读出)
String login_name="w";
boolean IsTeststop = false; //实现开始测试按钮复用的字段
boolean IsStartstop = false; //实现开始计步按钮复用的字段
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_footcount);
mHelper = new DBAdapter(this);
thistimecount = (TextView) findViewById(R.id.textView3);
havecount = (TextView) findViewById(R.id.textView4);
// 获取系统LocationManager服务
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
protected final LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// 当GPS定位信息发生改变时,更新位置
update_save_calculate(location);
}
@Override
public void onProviderDisabled(String provider) {
// 当GPS LocationProvider不可用时,不更新位置
update_save_calculate(null);
}
@Override
public void onProviderEnabled(String provider) {
// 当GPS LocationProvider可用时,更新位置
update_save_calculate(locationManager.getLastKnownLocation(provider));
}
@Override
public void onStatusChanged(String provider, int status,Bundle extras) {
}
};
//处理单击事件的代码
public void onTestClick(View v)
{
//取得开始时间
Time timepoint=new Time();
timepoint.setToNow(); //获得系统当前时间
int shour = timepoint.hour; // 0-23
int sminute = timepoint.minute;
int ssecond = timepoint.second;
int start_second = shour*3600 + sminute*60 + ssecond; //开始测试时间
//按钮复用
if(!IsTeststop)
{
//此时为开始状态,按钮应显示暂停
IsTeststop = true;
button_test.setText("暂停测试");
}
else
{
IsTeststop = false;
button_test.setText("开始测试");
}
while(IsTeststop)
{
/*
// 获取系统LocationManager服务
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
*/
// 从GPS获取最近的定位信息
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// 得到该点经纬度的距离并储存起来(初始值)
current_Longitude = location.getLongitude();
current_Latitude = location.getLatitude();
//每两秒更新一次GPS的位置信息
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 8,locationListener);
}
//移除监听器
locationManager.removeUpdates(locationListener);
//int time=1;
//需要记录开始测试到结束为止的时间代码
timepoint.setToNow(); //获得系统当前时间
shour = timepoint.hour; // 0-23
sminute = timepoint.minute;
ssecond = timepoint.second;
int end_second = shour*3600 + sminute*60 + ssecond; //结束测试时间
int time = end_second - start_second; //测试所用总时间
mDb = mHelper.getWritableDatabase();
//计算出平均一步走过的路程,并插入数据库中
percountleg = have_pasted/time;
ContentValues cv = new ContentValues(2);
//name1来着用户名,用户名由登陆的时候传进来,这里暂且用login_name表示
cv.put(DBAdapter.Name1, login_name);
cv.put(DBAdapter.Percount, percountleg);
mDb.insert(DBAdapter.TABLE_NAME1, null, cv);
};
activity_footcount.xml
<Button
android:id="@+id/button2"
android:onClick="onTestClick"
android:layout_width="150dip"
android:layout_height="40dip"
android:layout_marginLeft="90dip"
android:layout_marginTop="20dip"
android:text="开始测试"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:background="@drawable/shape" />
点击这个按钮后,app就停止服务了。然后报错。
logcat
01-13 19:01:05.041: E/MediaPlayer-JNI(5705): QCMediaPlayer mediaplayer NOT present
01-13 19:01:13.151: E/AndroidRuntime(5705): FATAL EXCEPTION: main
01-13 19:01:13.151: E/AndroidRuntime(5705): Process: com.example.sport, PID: 5705
01-13 19:01:13.151: E/AndroidRuntime(5705): java.lang.IllegalStateException: Could not execute method of the activity
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View$1.onClick(View.java:4028)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View.performClick(View.java:4788)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View$PerformClick.run(View.java:19911)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.os.Handler.handleCallback(Handler.java:739)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.os.Handler.dispatchMessage(Handler.java:95)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.os.Looper.loop(Looper.java:135)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.app.ActivityThread.main(ActivityThread.java:5269)
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Native Method)
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Method.java:372)
01-13 19:01:13.151: E/AndroidRuntime(5705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
01-13 19:01:13.151: E/AndroidRuntime(5705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
01-13 19:01:13.151: E/AndroidRuntime(5705): Caused by: java.lang.reflect.InvocationTargetException
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Native Method)
01-13 19:01:13.151: E/AndroidRuntime(5705): at java.lang.reflect.Method.invoke(Method.java:372)
01-13 19:01:13.151: E/AndroidRuntime(5705): at android.view.View$1.onClick(View.java:4023)
01-13 19:01:13.151: E/AndroidRuntime(5705): ... 10 more
01-13 19:01:13.151: E/AndroidRuntime(5705): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
01-13 19:01:13.151: E/AndroidRuntime(5705): at com.example.sport.FootCount.onTestClick(FootCount.java:252)
01-13 19:01:13.151: E/AndroidRuntime(5705): ... 13 more
我想问下,Caused by: java.lang.reflect.InvocationTargetException这个错误是什么意思啊?
还有Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference这个为什么是空指针?