简体   繁体   中英

Override not working in non-activity class

I dont know but why it is showing remove @Override annotation from onCreateContextMenu ? Anyone can tell my why its happening ?

I already check, my compiler is set to java 1.6. and my jre is set to 1.6.

contactPHP.java

    public class contactPHP extends AsyncTask<Object, Object, JSONArray>{
        final contactActivity main;
        private ListView listView;
        public contactPHP(contactActivity indv) {
            this.main = indv;
        }
    @Override
    protected void onPreExecute() {
         super.onPreExecute();  

            listView = (ListView)main.findViewById(R.id.listnum);
            adapter = new contactAdapter(main, imlist);
            listView.setAdapter(adapter);       
}
@Override      
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
           main.onCreateContextMenu(menu, v, menuInfo);  

        CNList obj = (CNList)v.getTag(); 
        int subs = obj.getsubstance();    

        menu.setHeaderTitle(obj.getname());  
        menu.add(0, v.getId(), 0, "Add");  
        menu.add(0, v.getId(), 0, "Remove");  
    }
    }

contactActivity.java

public class contactActivity extends Activity{



   @Override
   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.contact);
       getLayoutInflater().inflate(R.layout.contact, null);
       Intent intent = getIntent();
       new contactPHP(contactActivity.this).execute();
       //getNumber(contactActivity.this.getContentResolver()); 
   }



}

UPDATE 1

Added the onCreateContextMenu in activity and also returned onPostExecute() to activity but getting an error!

contactPHP.java edited

protected void onPostExecute(JSONArray json) {  
    main.contactPhpResponse(....);
}

contactActivity.java edited

  public void contactPhpResponse(ListView listView,JSONArray json){
      this.registerForContextMenu(listView);
     //some codes
  }
  @Override
   public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
       this.onCreateContextMenu(menu, v, menuInfo);  

    CNList obj = (CNList)v.getTag(); 
    int subs = obj.getsubstance();    

    menu.setHeaderTitle(obj.getname());  
        menu.add(0, v.getId(), 0, "Add");  
        menu.add(0, v.getId(), 0, "Remove");  
}

LogCAT

03-04 16:36:48.883: E/AndroidRuntime(5018): FATAL EXCEPTION: main
03-04 16:36:48.883: E/AndroidRuntime(5018): Process: com.example.soc, PID: 5018
03-04 16:36:48.883: E/AndroidRuntime(5018): java.lang.StackOverflowError
03-04 16:36:48.883: E/AndroidRuntime(5018):     at com.example.soc.contactActivity.onCreateContextMenu(contactActivity.java:92)
03-04 16:36:48.883: E/AndroidRuntime(5018):     at com.example.soc.contactActivity.onCreateContextMenu(contactActivity.java:92)
03-04 16:36:48.883: E/AndroidRuntime(5018):     at com.example.soc.contactActivity.onCreateContextMenu(contactActivity.java:92)
03-04 16:36:48.883: E/AndroidRuntime(5018):     at com.example.soc.contactActivity.onCreateContextMenu(contactActivity.java:92)

it means this line this.onCreateContextMenu(menu, v, menuInfo);

It's becuse onCreateContextMenu is not a method of AsyncTask , hence you can't override it. Move in your Activity class. Also, please, respect java naming conventions. Class name starts always with capital letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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