我现在有几天遇到这些问题,我不知道我的问题是什么。我现在真的需要一些帮助,跳跃可以帮助我在这里。这些是我的代码。
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);
}
}
服务类
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;
}}
跟踪课程
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);
}}
错误是:
java.lang.NullPointerException:尝试调用虚方法 &#39; android.view.View android.view.Window.findViewById(int)&#39;在null 对象参考
由于
答案 0 :(得分:0)
您正在从doInBackground返回null
值,这就是您获得NullPointerException
的原因。
使用以下代码更改MyAsyncTask
代码。
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);
}
}
答案 1 :(得分:0)
这是您的错误:
TextView text = (TextView) main.findViewById(R.id.id_trip);
text.setText(string);
你无法从TextView
获得Service
这样的对象。将TextView
对象创建为静态或使用BroadcastReceiver
。
答案 2 :(得分:0)
从setText(String str)
方法中删除以下2行。
MainActivity main = new MainActivity();
TextView text = (TextView) main.findViewById(R.id.id_trip);
希望这会对你有所帮助。