我正在使用MVP。我的活动包含Listview。我正在我的Activity的Create方法中初始化演示者,然后使用该presenter对象调用接口方法来获取数据并设置listview适配器。
我的问题是当presenter对象在任何onClick侦听器中使用然后工作正常但是当我直接使用它然后它在Presenter对象上给出NullPointerException。
这是我的代码段:
这是我的活动: -
public class EmployeeListActivity extends AppCompatActivity implements EmployeeContract.View {
private static final String TAG = EmployeeListActivity.class.getSimpleName();
private ListView listView;
private EmployeeContract.GetPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_list);
presenter = new EmployeePresenter(this); //--> Presenter inialization
}
@Override
public void initView() {
try {
listView = findViewById(R.id.listview);
presenter.getEmployees(listView); //--> interface method calling (Here I got exception.)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
presenter.listViewClick(position); //--> Same thing working perfectly here
}
});
} catch (Exception e) {
e.printStackTrace();
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
ErrorLog.saveLog(getBaseContext(), TAG, methodName, e.getMessage(), String.valueOf(e.getCause()));
}
}
@Override
protected void onResume() {
super.onResume();
presenter = new EmployeePresenter(this);
}
}
帮助将受到关注。提前谢谢。
更新
这是我的错误日志
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.myfirstexamplemvp.contract.EmployeeContract$GetPresenter.getEmployees(android.widget.ListView)' on a null object reference
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at com.example.myfirstexamplemvp.view.EmployeeListActivity.initView(EmployeeListActivity.java:35)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at com.example.myfirstexamplemvp.presenter.EmployeePresenter.initPresenter(EmployeePresenter.java:40)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at com.example.myfirstexamplemvp.presenter.EmployeePresenter.<init>(EmployeePresenter.java:34)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at com.example.myfirstexamplemvp.view.EmployeeListActivity.onCreate(EmployeeListActivity.java:28)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.Activity.performCreate(Activity.java:6582)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:111)
01-29 16:11:54.709 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.os.Looper.loop(Looper.java:207)
01-29 16:11:54.710 28540-28540/com.example.myfirstexamplemvp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5776)
01-29 16:11:54.710 28540-28540/com.example.myfirstexamplemvp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-29 16:11:54.710 28540-28540/com.example.myfirstexamplemvp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-29 16:11:54.710 28540-28540/com.example.myfirstexamplemvp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
以下是我的演示者: -
public class EmployeePresenter implements EmployeeContract.AddPresenter, EmployeeContract.GetPresenter, EmployeeContract.EditPresenter {
private EmployeeContract.View view;
AddEmployeeModel model;
SqliteController controller;
public EmployeePresenter(EmployeeContract.View view) {
this.view = view;
initPresenter();
}
private void initPresenter() {
model = new AddEmployeeModel();
controller = new SqliteController((Context) view);
view.initView();
}
@Override
public void getEmployees(final ListView listView) {
List<AddEmployeeModel> list = controller.getEmployeeList();
ArrayList<String> empname = new ArrayList<String>();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
empname.add(list.get(i).getFirstName() + " " + list.get(i).getLastName());
}
listView.setAdapter(new ArrayAdapter<String>((Context) view, android.R.layout.simple_expandable_list_item_1, empname));
}
}
}
答案 0 :(得分:1)
试试这个:
getTripsLocation
进入演示者
public class EmployeeListActivity extends AppCompatActivity implements EmployeeContract.View {
private static final String TAG = EmployeeListActivity.class.getSimpleName();
private ListView listView;
private EmployeeContract.GetPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_list);
presenter = new EmployeePresenter(this);
}
@Override
public void initView(List<Employee> employees, Presenter.Callback callback) {
listView = findViewById(R.id.listview);
EmployeesAdapter adapter = new EmployeesAdapter(this, employees);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
callback.onClick(position);
}
})
}
@Override
protected void onResume() {
super.onResume();
//presenter = new EmployeePresenter(this);
}
答案 1 :(得分:0)
我认为你的代码违反MVP.your Presenter不应该包含任何Android代码。这就是MVP的强大功能,便于测试。
1 - 在init()方法中初始化listView,包括设置适配器。
public void initView() {
listView = findViewById(R.id.listview);
adapter = new EmployeesAdapter(this, employeesList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
presenter.listItemWasClicked(position).
}
})
}
2 - 创建一个名为EmployeeData的接口。
public interface EmployeeData {
void add(Employee employee);
Employee getEmployee(int position);
int getSize();
}
适配器内的3 - 实现EmployeeData 。
{
//.....adapter methods
@Overrride
public Employee getEmployee(int position){
return myEmployeeList.get(position);
}
//.. other methods.
}
在演示者中4 -
private EmployeeData empData;
public EmployeePresenter (View view,EmployeeData empData){
this.empData=empData;
//... other init
}
@Override
public void listItemWasClicked(int position){
Employee emp=empData.getEmployee(position)
}
5 - 在您onCreate()
的活动中,例如,初始化您的演示者。
employeePresenter=new EmployeePresenter(this,listAdapter);
注意:在制作预告中之前不要忘记致电initView()