我有一个名为“temp”的项目,我在其中实现了一些类及其方法。
然后我创建了另一个名为“temp2”的项目,并包含了“temp”库,以便使用我在那里实现的类。
现在,如果我在“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
}
我想在 SwipeListView
类中使用 listAdapter
变量,但是每当我想在 SwipeListView
中调用适配器的方法时,我收到一个 NullPonterException
最佳答案
如果变量位于其他类上:
- 导入其他类
- 并将它们放在类路径中
如何设置类路径:
单击项目 --> 属性 --> Java 构建路径 --> (此处您可以使用“添加 JAR”或“添加外部 JAR”),具体取决于系统中 JAR 文件的物理位置。
关于java - 在另一个项目中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15588469/