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

时间:2012-11-23 05:25:44

标签: android fragment android-context

当我使用getApplicationContext()时,eclipse会给我错误create method 'getApplicationContext()'。请告诉我如何删除此错误。我也尝试使用 ClassName.this 那个时间错误,例如change method startListening(Context) to startListening(Main)

当我使用时:measurement_index = AppSettings.getMeasureUnit(this);  然后出现错误:change method getMeasureUnit(Context) to getMeasureUnit(Main)

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

  1. 创建方法'getApplicationContext()'

  2. 将方法getMeasureUnit(Context)更改为getMeasureUnit(Main)

  3. 检查我的代码。

    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);
    }
    

1 个答案:

答案 0 :(得分:5)

在片段中,您可以改为使用getActivity().getApplicationContext()

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