I'm stuck with these problem several day now, I don't know what is my problem is. I really need some help now, hopping can help me out here. These is my code.
public class MainActivity extends AppCompatActivity implements LocationListener {
TextView text;
String trip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.id_trip);
scheduleAlarm();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
public void onLocationChanged(Location location) {
new MyAsyncTask ().execute();
}
public class MyAsyncTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
trip = "Hai";
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {
text.setText(trip);
}
}
public void scheduleAlarm() {
Intent intent = new Intent(getApplicationContext(), Track.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, Track.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 60 * 1000, pIntent);
}
}
Services class
public class Services extends Service {
public LocationManager locationManager;
public MainActivity listener;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MainActivity();
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, listener, null);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}}
Track class
public class Track extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Services.class);
context.startService(i);
}}
And the error are :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
Thanks
You are returning null
value from doInBackground, that is the reason you are getting NullPointerException
.
Change MyAsyncTask
code with the following code.
public class MyAsyncTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
if(text==null){
text = MainActivity.this.findViewById(R.id.id_trip);
}
}
@Override
protected String doInBackground(String... arg0) {
trip = "Hai";
return trip;
}
@Override
protected void onPostExecute(String strFromDoInBg) {
text.setText(strFromDoInBg);
}
}
Here is your error :
TextView text = (TextView) main.findViewById(R.id.id_trip);
text.setText(string);
you cannot get TextView
object like this from Service
. Either create TextView
object as static or use BroadcastReceiver
.
Remove below 2 lines from setText(String str)
methood.
MainActivity main = new MainActivity();
TextView text = (TextView) main.findViewById(R.id.id_trip);
Hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.