我正在尝试复制列表' usrs'这是在Inner类中创建的一个不同的列表' team_memebers'。 复制后,我尝试迭代“team_memebers'在FOR循环中,但是我得到了一个' null对象引用'错误。 '用户' list包含返回的对象,通过调试打印进行测试。
public class ListNodeActivity extends AppCompatActivity
{
private ParseObject parse_task=null;
private List<String> team_memebers=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_node);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> usrs, ParseException e) {
if (e == null) {
team_memebers = new ArrayList<String>(usrs.size());
for (ParseObject prso:usrs) {
team_memebers.add(new String(prso.getString("Username")));
}
} else {//handle the error
}
}
});
for (String str:team_memebers)
{
empolyeeSpinnerAdapter.add(str);
}
}
堆栈跟踪
FATAL EXCEPTION: main
Process: il.ac.shenkar.david.todolistex2, PID: 14490
java.lang.RuntimeException: Unable to start activity ComponentInfo{il.ac.shenkar.david.todolistex2/il.ac.shenkar.david.todolistex2.ListNodeActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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 interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
at il.ac.shenkar.david.todolistex2.ListNodeActivity.onCreate(ListNodeActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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 :(得分:4)
你的问题是你在复制列表后没有迭代。
query.findInBackground()
是异步回调,因此不会立即执行。因为你的迭代循环在回调下面是 PLACED ,所以并不意味着它将在回调执行后执行。只需将你的循环置于回调中:
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> usrs, ParseException e) {
if (e == null) {
team_memebers = new ArrayList<String>(usrs.size());
for (ParseObject prso:usrs) {
team_memebers.add(new String(prso.getString("Username")));
}
for (String str:team_memebers)
{
empolyeeSpinnerAdapter.add(str);
}
} else {//handle the error
}
}
});