我收到错误'尝试调用方法' ...'在null对象引用'在Android上尝试访问之前设置的字段时。 我检查设置它不是空的,但是当它在不同的活动中被访问时,它“无效”。 该字段与多个其他字段一起创建。并且出于某种原因,它是唯一一个看似旧的空引用。
有问题的对象
public enum Locations {
Meeting_Room,Office_245,Lobby,NOC,VPsoffice
}
班级声明:
public class Task implements Serializable {
private long taskId;
private String description;
private Boolean completed = false;
private Category task_catg;
private Task_Status task_sts;
private Locations tsk_location;
}
public Locations getTsk_location() {
return tsk_location;
}
public void setTsk_location(Locations tsk_location) {
this.tsk_location = tsk_location;
}
请注意 Task_Status & 类别也是枚举,我不会遇到任何问题。
这是对象初始化的地方,我在这里添加了打印,并看到活动返回的值不为null。 新任务活动
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK) {
switch (requestCode) {
case ACTIVITY_SELECT_LOCATION:
{
/*returned_selc_loc = (Locations) data.getSerializableExtra("location");
loc.setText(returned_selc_loc.toString());
loc.setClickable(false);*/
switch(Globals.temp)
{
case 0:
returned_selc_loc=Locations.Meeting_Room;
loc.setText(returned_selc_loc.toString());
loc.setClickable(false);
break;
case 1:
returned_selc_loc=Locations.Office_245;
loc.setText(returned_selc_loc.toString());
loc.setClickable(false);
break;
case 2:
returned_selc_loc=Locations.Lobby;
loc.setText(returned_selc_loc.toString());
loc.setClickable(false);
break;
case 3:
returned_selc_loc=Locations.NOC;
loc.setText(returned_selc_loc.toString());
loc.setClickable(false);
break;
case 4:
returned_selc_loc=Locations.VPsoffice;
loc.setText(returned_selc_loc.toString());
loc.setClickable(false);
break;
}
break;
}
default:
break;
}
}
}
这是创建Task对象并设置Location的位置: 新任务活动
public void addTaskBtn (View view)
{
boolean state=true;
EditText desc = (EditText)findViewById(R.id.newTaskDesc);
EditText date = (EditText)findViewById(R.id.taskDateEdit);
EditText time = (EditText)findViewById(R.id.taskTimeEdit);
loc = (EditText)findViewById(R.id.taskLocation);
Date myDate = null;
RadioButton rb;
String emp_name;
t = new Task(desc.getText().toString());
t.setTask_sts(Task_Status.WAITING);
int position = spin.getSelectedItemPosition();
switch(position)
{
case 0:
t.setTask_catg(Category.GENERAL);
break;
case 1:
t.setTask_catg(Category.CLEANING);
break;
case 2:
t.setTask_catg(Category.ELECTRICITY);
break;
case 3:
t.setTask_catg(Category.COMPUTERS);
break;
case 4:
t.setTask_catg(Category.OTHER);
break;
}
//Added prints here and value is set correctly, definitely not NULL.
t.setTsk_location(returned_selc_loc);
t.setTaskId(task_id);
task_id++;
Intent returnIntent = new Intent();
dbm = DBManager.getInstance(this);
long seq_tsk_id = dbm.addTask(t);
t.setTaskId(seq_tsk_id);
returnIntent.putExtra("task", t);
setResult(RESULT_OK, returnIntent);
finish();
}
这是对编辑任务活动的调用,该调用来自MainActivity。
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbM = DBManager.getInstance(context);
itemList = dbM.getAllTasks();
list.setAdapter(new TaskItemAdapter(context, itemList));
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long arg3) {
//get item instance from list
Task tt = (Task) ((TaskItemAdapter) parent.getAdapter()).getItem(position);
if(true)
{
//start the create activity again, now for editing
Intent i = new Intent(getApplicationContext(), EditTaskActivity.class);
i.putExtra("task", tt);
startActivityForResult(i, REQUEST_CODE_UPDATE_TASK);
}
return false;
}
});
}
修改任务活动 这是我得到错误(只留下相关部分,因为对象中的其他变量成功,
public void deleteTaskBtn(View view)
{
Intent returnIntent = new Intent(this,MainActivity.class);
Intent i = getIntent();
tastToEdit = (Task)i.getSerializableExtra("task");
tastToEdit.setToDelete(true);
ParseObject parse_task = new ParseObject("Task");
parse_task.put("Description",tastToEdit.getDescription());
parse_task.put("DueDate",tastToEdit.getDueDate());
parse_task.put("Priority",tastToEdit.getPriority().ordinal());
int com_state = (tastToEdit.getCompleted()) ? 1 : 0;
parse_task.put("IsCompleted",com_state);
parse_task.put("Location", tastToEdit.getTsk_location().ordinal());
parse_task.put("Category",tastToEdit.getTask_catg().ordinal());
parse_task.put("Status", tastToEdit.getTask_sts().ordinal());
parse_task.put("TeamName",Globals.team_name);
parse_task.put("Employee",tastToEdit.getEmp_name());
parse_task.deleteInBackground(new DeleteCallback() {
public void done(ParseException e) {
if (e == null) {
Log.d("msg","deleted");
} else {
Log.d("msg", "not deleted");
e.printStackTrace();
}
}
});
returnIntent.putExtra("task",tastToEdit);
setResult(RESULT_OK, returnIntent);
finish();
}
该行返回NULL引用:
Location selected_loc = tastToEdit.getTsk_location();
完整错误堆栈
03-09 21:28:20.192 10425-10425/? I/art: Late-enabling -Xcheck:jni
03-09 21:28:20.229 10425-10433/? I/art: Debugger is no longer active
03-09 21:28:20.240 10425-10425/? W/System: ClassLoader referenced unknown path: /data/app/il.ac.shenkar.david.todolistex2-1/lib/arm
03-09 21:28:20.446 10425-10456/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-09 21:28:20.508 10425-10456/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
03-09 21:28:20.511 10425-10456/? I/OpenGLRenderer: Initialized EGL, version 1.4
03-09 21:28:27.832 10425-10425/il.ac.shenkar.david.todolistex2 I/Choreographer: Skipped 52 frames! The application may be doing too much work on its main thread.
03-09 21:28:28.397 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0xb38ccf80 (RippleDrawable) with handle 0xaef7fe30
03-09 21:28:29.620 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0xb39ba400 (RippleDrawable) with handle 0xaef7f9e0
03-09 21:28:30.360 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0xb397f900 (RippleDrawable) with handle 0xb3b29370
03-09 21:28:32.921 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0x9f2cbb80 (ListPopupWindow$DropDownListView) with handle 0xb3b29530
03-09 21:28:33.437 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0x9f2cad80 (RippleDrawable) with handle 0xb3b295d0
03-09 21:28:40.866 10425-10456/il.ac.shenkar.david.todolistex2 V/RenderScript: 0x9dca9000 Launching thread(s), CPUs 4
03-09 21:28:40.907 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0xb3947700 (RippleDrawable) with handle 0xb3b29910
03-09 21:28:44.971 10425-10425/il.ac.shenkar.david.todolistex2 W/sdas: null
03-09 21:28:45.143 10425-10456/il.ac.shenkar.david.todolistex2 D/OpenGLRenderer: endAllStagingAnimators on 0xb39b5380 (ListView) with handle 0xaef7f510
03-09 21:28:48.110 10425-10425/il.ac.shenkar.david.todolistex2 D/AndroidRuntime: Shutting down VM
03-09 21:28:48.111 10425-10425/il.ac.shenkar.david.todolistex2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: il.ac.shenkar.david.todolistex2, PID: 10425
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int il.ac.shenkar.david.todolistex2.Location.ordinal()' on a null object reference
at il.ac.shenkar.david.todolistex2.EditTaskActivity.deleteTaskBtn(EditTaskActivity.java:372)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
但正如您所看到的,此字段先前已在另一项活动中成功设置。
可能是什么原因?
答案 0 :(得分:0)
在我看来,这不是关于该字段是否已经设置,而是你试图拉出意图的对象并不在{{1 }}:
onCreate
在此之后添加一个检查以查看tastToEdit是否为null,如果是,请调查为什么它没有正确添加到intent中,如果是这种情况。
答案 1 :(得分:0)
您似乎错误地认为您在一个活动onActivityResult()中填充的意图会发生什么。该方法负责处理使用startActivityForResult()调用的下一个活动的结果。下一个活动可以使用setResult()设置先前活动的结果,当调用的活动完成时,该结果在onActivityResult()中可用。
然而,您似乎期待&#34;另一种活动&#34;中的getIntent()。能够看到你在onActivityResult()中创建的Intent。事实并非如此。 getIntent()返回用于启动您正在使用的当前活动的活动。它与活动结果的意图完全无关。因此,您需要更改在这些活动之间传递数据的策略。