1

我有一个名为“temp”的项目,我实现了一些类及其方法。

然后我创建了另一个名为“temp2”的项目,并包含了库“temp”,以便使用我在那里实现的类。

现在,如果我在“临时”项目中创建了一个类:

private SwipeListView swipeListView;   // SwipeListView  is a class declared in "temp2" project
private MyCustomAdapter adapter;   // class MyCustomAdapter is a class in "temp" project


swipeListView = (SwipeListView) findViewById(R.id.example_lv_list);
adapter =new MyCustomAdapter(text,listImages);  

swipeListView.setListenerAdaper(adapter);

adapter.someMethod();   //doesnt give me a nullpointerException

在类中SwipeListView,我声明了方法setListenerAdaper(BaseAdpter)useAdapter()

private BaseAdapter listAdapter;

public void setListenerAdaper(BaseAdapter baseAdapter) {
    listAdapter = baseAdapter;
}

public void tempmethod() {
    useAdapter();
}

public void useAdapter() {
   listAdapter.someMethod();  //NullPointerException
}

我想listAdapter在类中使用变量SwipeListView,但是每当我想在类中调用适配器的方法时SwipeListView,我都会得到一个NullPonterException

4

2 回答 2

0

If you need to import something from other project I think you need to consider a better architecture.

To achieve that you need to create a jar of the other project, make that variable a class and public one, and finally import it from the class you want to use it.

But as I mentioned before, you need to check your architecture in order to have a library with all the classes that will be used by several projects.

于 2013-03-23T15:45:21.020 回答
0

如果变量在其他类上:

  1. 导入其他类
  2. 并将它们放在类路径上

如何设置类路径:

根据您的 JAR 文件在系统中的物理位置,单击 Project --> Properties --> Java Build Path -->(在这里您可以使用“Add JARs”或“Add External JARs”)。

于 2013-03-23T15:33:25.390 回答