我试图在点击按钮时运行一个线程,但强行关闭,
这是代码,
package kulkarni.darpan.vtc;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnRegister;
EditText emailRegister;
CheckBox enDisTracking;
Handler hand;
GPSTracker gps;
AsyncPostRequest asp;
AsyncRegisterDevice ard;
DeviceInfo di;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps = new GPSTracker(MainActivity.this);
di = new DeviceInfo(MainActivity.this);
asp = new AsyncPostRequest();
ard = new AsyncRegisterDevice();
btnRegister = (Button) findViewById(R.id.regButton);
emailRegister = (EditText)findViewById(R.id.regEmail);
enDisTracking = (CheckBox)findViewById(R.id.startTracking);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//registerDevice();
hand.postDelayed(run, 1000);
}
});
}
Runnable run = new Runnable() {
public void run()
{
sendLocation();
}
};
public void registerDevice(){
String regEmail = emailRegister.getText().toString();
String regUniqueID = di.getDeviceInfo();
String regObj[] = new String[2];
regObj[0] = regEmail;
regObj[1] = regUniqueID;
ard.execute(regObj);
}
public void sendLocation(){
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String uniq = di.getDeviceInfo();
String aobj[] = new String[3];
aobj[0]=Double.toString(latitude);
aobj[1]=Double.toString(longitude);
aobj[2]=uniq;
asp.execute(aobj);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude +"\nIMEI: "+uniq, Toast.LENGTH_LONG).show();
}else{
gps.showSettingsAlert();
}
hand.postDelayed(run, 20000);
}
}
这是日志文件,
02-19 17:57:29.115: D/Network(29198): Network
02-19 17:57:29.275: D/libEGL(29198): loaded /system/lib/egl/libEGL_mali.so
02-19 17:57:29.305: D/libEGL(29198): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-19 17:57:29.310: D/libEGL(29198): loaded /system/lib/egl/libGLESv2_mali.so
02-19 17:57:29.315: E/(29198): Device driver API match
02-19 17:57:29.315: E/(29198): Device driver API version: 23
02-19 17:57:29.315: E/(29198): User space API version: 23
02-19 17:57:29.315: E/(29198): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
02-19 17:57:29.425: D/OpenGLRenderer(29198): Enabling debug mode 0
02-19 17:57:38.575: D/AndroidRuntime(29198): Shutting down VM
02-19 17:57:38.575: W/dalvikvm(29198): threadid=1: thread exiting with uncaught exception (group=0x41cd1700)
02-19 17:57:38.580: E/AndroidRuntime(29198): FATAL EXCEPTION: main
02-19 17:57:38.580: E/AndroidRuntime(29198): java.lang.NullPointerException
02-19 17:57:38.580: E/AndroidRuntime(29198): at kulkarni.darpan.vtc.MainActivity$2.onClick(MainActivity.java:41)
02-19 17:57:38.580: E/AndroidRuntime(29198): at android.view.View.performClick(View.java:4475)
02-19 17:57:38.580: E/AndroidRuntime(29198): at android.view.View$PerformClick.run(View.java:18786)
02-19 17:57:38.580: E/AndroidRuntime(29198): at android.os.Handler.handleCallback(Handler.java:730)
02-19 17:57:38.580: E/AndroidRuntime(29198): at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 17:57:38.580: E/AndroidRuntime(29198): at android.os.Looper.loop(Looper.java:176)
02-19 17:57:38.580: E/AndroidRuntime(29198): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-19 17:57:38.580: E/AndroidRuntime(29198): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 17:57:38.580: E/AndroidRuntime(29198): at java.lang.reflect.Method.invoke(Method.java:525)
02-19 17:57:38.580: E/AndroidRuntime(29198): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-19 17:57:38.580: E/AndroidRuntime(29198): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-19 17:57:38.580: E/AndroidRuntime(29198): at dalvik.system.NativeStart.main(Native Method)
有关此问题的任何解决方案吗?
答案 0 :(得分:2)
您尚未初始化Handler hand
。你只宣布它。确保在使用之前初始化Handler。
hand = new Handler();
看看@
http://developer.android.com/reference/android/os/Handler.html