其他分享
首页 > 其他分享> > android – 如何删除错误:扩展Fragment时创建方法’getApplicationContext()’和’runOnUiThread(Runnable)’

android – 如何删除错误:扩展Fragment时创建方法’getApplicationContext()’和’runOnUiThread(Runnable)’

作者:互联网

当我使用getApplicationContext()时eclipse给我错误创建方法’getApplicationContext()’.请告诉我如何删除此错误.我也尝试使用ClassName.this时间错误,如更改方法startListening(Context)到startListening(Main)

当我使用时:measurement_index = AppSettings.getMeasureUnit(this);
 那么错误就像:将方法getMeasureUnit(Context)更改为getMeasureUnit(Main)

所以请让我知道如何解决错误:

> create method’getApplicationContext()’
>将方法getMeasureUnit(Context)更改为getMeasureUnit(Main)

检查我的代码.

Main.class

public class Main extends Fragment implements GPSCallback{

....... 

public static Fragment newInstance(int position) {
    Main f = new Main();
    Bundle args = new Bundle();

     ...

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final ViewGroup root = (ViewGroup) inflater.inflate(R.layout.main, null);

    /*Intent in = new Intent(Main.this,MainDisplay.class);
              // ------------> Here give error like : Remove arguments match to Intent();
    startActivity(in);*/

    tv_display_Speed = (TextView) root.findViewById(R.id.speed_display);

     gpsManager = new GPSManager();

        gpsManager.startListening(getApplicationContext());
                   //------------>  Eclipse give me error create method 'getApplicationContext()'

        gpsManager.setGPSCallback(this);

        //(TextView)root.findViewById(R.id.speed_display);

        measurement_index = AppSettings.getMeasureUnit(this);
                   //------------> Here Give me error like : change method getMeasureUnit(Context) to  getMeasureUnit(Main) 


    btnstart = (Button) root.findViewById(R.id.button_start);
    btnstart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        }
    });

    return root;
}

startListening方法:

public void startListening(final Context context)
{
     ....
}

getMeasureUnit方法:

public static int public static int getMeasureUnit(Context context){
    return getInt(context,AppSettings.UNIT_STRING);
} 

编辑:

我在上面的类中添加了onStart()方法,扩展了fragment.
那个时候在runOnUiThread(updateTask); Eclipse给我的错误如下:create method runOnUiThread(Runnable).请让我知道如何实现这一点.

@Override
public void onStart() {
    super.onStart();
    timer = new Timer("DigitalClock");
    Calendar calendar = Calendar.getInstance();

    // Get the Current Time
    final Runnable updateTask = new Runnable() {
        public void run() {
            txt_display_clock.setText(getCurrentTimeString()); // shows the current
                                                        // time of the day

        }
    };

    // update the UI
    int msec = 999 - calendar.get(Calendar.MILLISECOND);
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(updateTask); // <---------------- HERE
        }
    }, msec, 1000);
}

解决方法:

在Fragment中,您可以使用getActivity().getApplicationContext()代替.

对于AppSettings.getMeasureUnit错误,如果getMeasureUnit方法需要上下文,则通常可以使用getActivity().getApplicationContext().

标签:android,android-context,fragment
来源: https://codeday.me/bug/20191008/1874181.html