从不同包中的类调用公共类的公共方法

时间:2013-05-21 15:19:28

标签: java

我试图在不同的包中调用类的公共方法。 我已经导入了包,所以这不是问题。我正在使用eclipse,它给了我下面提到的错误。

代码:

public class BookView extends LinearLayout
   implements TextToSpeech.OnInitListener
   {
   private static final String LOGTAG = "BookView";
   private EPubWebView afd_webView;
   private Context afd_curContext;
  private FrameLayout afd_tableContentView;
  private String afd_cachePath = "";
  private EPubDriver afd_epubDriver;
  private AdView adView;
  private int currentPageNum = 1;
  private int totalPages;
  private int current_chapter;
  private int current_percent;
  private int pIndex;
  private int sIndex;
  private int size;
  private int chapterSize;
  private int bookSize;
  private String clickBk;
  private String bookPath;
  private String mDir = "/sdcard";
  private List<Map<String, Object>> mDataList;
  private ListView bookListView;
  private Handler handler = new Handler();
  private PopupWindow mBooksWindow;
  private int andVersion;
  private TextToSpeech tts;
  private final String errorMessage = "Download Error!";
  private boolean downloadCancel = false;
  private boolean isDownloading = false;

  public BookView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    setOrientation(1);
    this.afd_curContext = context;
    this.handler.postDelayed(new Runnable()
    {
      public void run()
      {
    BookView.this.doAdMob();
      }
    }
    , 10000L);

    this.tts = new TextToSpeech(context, this);

    this.afd_webView = new EPubWebView(context);
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity)context).getWindowManager().getDefaultDisplay()
      .getMetrics(metrics);

    LayoutInflater inflater = (LayoutInflater)context
  .getSystemService("layout_inflater");
    inflater.inflate(R.layout.afd_bookview, this);
    init();
  }

  public void jsImportBook(String epubBookPath) {
  if (!BookView.this.importBook(epubBookPath))
    return;
  BookView.this.createBookshelf();
}
}

下面的调用方法是否正确?

 public void importnewbook(String epubBookPath) {

BookView v = new BookView();
    result = v.jsImportBook(epubBookPath); 
}

我得到的错误: 1)构造函数BookView()未定义 2)对于类型BookView

,方法jsImportBook(String)未定义

2 个答案:

答案 0 :(得分:2)

请记住,默认情况下,如果您自己没有提供任何参数,编译器将添加一个no-args构造函数。

但是如果你添加一个,假设它接受一个字符串,那么编译器假定你知道你在做什么而不采取任何行动。这意味着你不会有一个no-args构造函数,如果你想要一个你需要定义它。

简而言之:

1)什么都不做,你得到一个no-args构造函数。 2)添加任何构造函数,这就是你所拥有的,编译器不会添加任何其他内容。

当然,请务必检查您定义的方法是否返回您期望的类型。

答案 1 :(得分:1)

  1. 检查您的导入声明。
  2. 你班上有参数化的构造函数吗?如果是,请添加默认构造函数。
  3. 我可以看到你的类有一个参数化的构造函数。因此,在这种情况下,不会为您创建默认构造函数。您必须自己编写一个才能编写新的BookView()。

相关问题