2

When I am using the getApplicationContext() that time eclipse give me error create method 'getApplicationContext()'. Please let me know How to remove this error. I am try also using ClassName.this that time error like change method startListening(Context) to startListening(Main)

Also when I use like : measurement_index = AppSettings.getMeasureUnit(this); then Error like :change method getMeasureUnit(Context) to getMeasureUnit(Main)

So please let me know how can solve error like:

  1. create method 'getApplicationContext()'

  2. change method getMeasureUnit(Context) to getMeasureUnit(Main)

Check my code.

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 method:

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

getMeasureUnit method:

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

EDIT :

I have Added the onStart() method into above class that extends fragment. That time at runOnUiThread(updateTask); Eclipse give me error like : create method runOnUiThread(Runnable) .Please let me know how can I achieve this.

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

1 回答 1

5

In a Fragment, you can use getActivity().getApplicationContext() instead.

As for the AppSettings.getMeasureUnit error, if the getMeasureUnit method is expecting a context you can usually use getActivity().getApplicationContext().

于 2012-11-23T05:27:31.633 回答