简体   繁体   English

Android更改TextView文本

[英]Android Change TextView text

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 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.Window.findViewById(int)”

Thanks 谢谢

You are returning null value from doInBackground, that is the reason you are getting NullPointerException . 您正在从doInBackground返回null值,这就是得到NullPointerException的原因。

Change MyAsyncTask code with the following code. 用以下代码更改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);
    }
}

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 . 您不能从Service获得像这样的TextView对象。 Either create TextView object as static or use BroadcastReceiver . TextView对象创建为静态对象,或者使用BroadcastReceiver

Remove below 2 lines from setText(String str) methood. setText(String str)删除以下两行。

MainActivity main = new MainActivity();
TextView text = (TextView) main.findViewById(R.id.id_trip);

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM