簡體   English   中英

Android-嘗試在空對象引用上調用方法“ ...”

[英]Android - Attempt to invoke method '…' on a null object reference

嘗試訪問先前設置的字段時,Android出現錯誤“嘗試在空對象引用上調用方法'...'”。 我檢查是否將其設置為非null,但是在其他活動中訪問它時,它為“ null”。 此字段是與其他多個字段一起創建的,由於某種原因,它是唯一一個顯示空引用的字段。

有問題的對象

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_StatusCategory也是枚舉,我沒有遇到任何問題。

這是初始化對象的地方,我在這里添加了打印件,並看到從活動返回的值不為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的地方: New Task活動

    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(); 
}

這是對Edit Task活動的調用,該調用來自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) 

但是如您所見,該字段是先前在另一個活動中成功設置的。

可能是什么原因?

在我看來,這與是否已設置字段無關,而是您試圖實現意圖的對象不在onCreate這一行上:

tastToEdit = (Task)i.getSerializableExtra("task");

在此之后添加檢查以查看tastToEdit是否為null,如果是,請調查為什么未將其正確添加到意圖中。

您似乎對在一個Activity的onActivityResult()中填充的意圖發生了什么誤解。 該方法負責處理使用startActivityForResult()調用的下一個活動的結果。 該下一個活動可以使用setResult()為上一個活動設置結果,當調用的活動完成時,該活動將在onActivityResult()中可用。

但是,您似乎希望“另一個活動”中的getIntent()能夠看到您在onActivityResult()中創建的Intent。 事實並非如此。 getIntent()返回用於啟動您正在使用的當前活動的活動。 這與活動結果的意圖完全無關。 因此,您需要更改在這些活動之間傳遞數據的策略。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM