覆盖不在非活动类中工作

时间:2015-03-03 19:26:51

标签: java android class android-activity override

我不知道为什么它会显示从onCreateContextMenu删除@Override注释? 任何人都可以告诉我为什么会这样?

我已经检查过,我的编译器设置为java 1.6。我的jre设置为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()); 
   }



}

更新1

在活动中添加了onCreateContextMenu,并将onPostExecute()返回给活动,但收到了错误!

contactPHP.java 已修改

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

contactActivity.java 已编辑

  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)

表示此行this.onCreateContextMenu(menu, v, menuInfo);

1 个答案:

答案 0 :(得分:4)

这是onCreateContextMenu不是AsyncTask的方法,因此你无法覆盖它。移入Activity课程。另外,请尊重java命名约定。班级名称始终以大写字母开头。