我正在使用MVP。我的活动包含一个Listview。我正在使用Activity的on Create方法初始化演示者,然后使用该演示者对象调用接口方法以获取数据并设置listview适配器。
我的问题是,在任何onClick侦听器中使用presenter对象时,它们都能正常工作,但是当我直接使用它时,它将在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 reference01-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));
}
}
}
最佳答案
尝试这个:
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);
}
进入演示者
public EmployeePresenter(EmployeeView view) {
List<Employee> employees = ...; // get the list of employees
Callback callback = new Callback(){
void onClick(int position){
listViewClick(position);
}
}
view.initView(employees, callback);
}
public interface Callback{
void onClick(int position);
}
关于java - MVP(Android)中的演示者对象获取空对象引用错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48499790/