Java 将变量从活动传递到适配器
我想将一个变量从活动传递到适配器。Java 将变量从活动传递到适配器,java,android,Java,Android,我想将一个变量从活动传递到适配器。我不知道哪里出错了 public class CustomListAdapter extends BaseExpandableListAdapter { private int weight; public void setWeight(int weight) { this.weight = weight; } ..... } 但我无法在活动中使用此方法 public class mActivity extends Ac
我不知道哪里出错了
public class CustomListAdapter extends BaseExpandableListAdapter {
private int weight;
public void setWeight(int weight) {
this.weight = weight;
}
.....
}
但我无法在活动中使用此方法
public class mActivity extends Activity {
CustomListAdapter expandableListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emergency_activity);
expandableListAdapter = new CustomListAdapter (this);
//expandableListAdapter.setWeight(5);
...
“expandableListAdapter无法解析为类型”您是否将变量声明为一种类型
CustomListAdapter expandableListAdapter;
然后尝试将其实例化为另一个实例
expandableListAdapter = new EmergencyListAdapter(this);
这可能会产生几个不同的错误,但在这里,您的IDE认为它是一个没有声明类型的新变量
您需要将声明的类型更改为
EmergencyListAdapter
,或者将其实例化为new CustomListAdapter()
(假设该类具有空构造函数)。未创建exapndableListAdapter。您正在创建EmergencyListAdapter(尚未提供代码)
您要做的可能是:
expandableListAdapter = new CustomListAdapter(this);
那么你应该能够做到:
expandableListAdapter.setWeight(5);
祝你好运。你的意思是让
expandableListAdapter=新的CustomListAdapter(这个)代码>?您正在将其声明为一种类型,然后尝试将其实例化为另一种类型。您应该将其作为答案发布bro@codeMagicCan您可以发布完整的stacktrace吗?抱歉,这是我在处理文本时犯的错误。这个问题仍然存在。编辑我的问题。您仍然会遇到这个错误,“expandableListAdapter无法解析为类型”?你还有其他错误吗?有拼写错误。从几小时后就开始找了。无论如何,谢谢!:)