所以,我试图从所有解析用户那里获取用户名列表并将其显示在列表视图中,这样我就可以在我的应用程序中搜索任何人,但我收到了这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.les20162.room8, PID: 19957
java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at com.les20162.room8.Activities.RoomatesFragment.initList(RoomatesFragment.java:171)
at com.les20162.room8.Activities.RoomatesFragment.onCreateView(RoomatesFragment.java:81)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
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:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是我用来获取用户列表的代码:
public class RoomatesFragment extends Fragment {
//variables
private EditText search;
private String[] users;
private ArrayList<String> listUsers;
private ArrayAdapter<String> adapter;
private ListView listView;
private OnFragmentInteractionListener mListener;
public RoomatesFragment() {
// Required empty public constructor
}
public static RoomatesFragment newInstance(String param1, String param2) {
RoomatesFragment fragment = new RoomatesFragment();
Bundle args = new Bundle();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View myView;
myView = inflater.inflate(R.layout.fragment_roomates, container, false);
listView = (ListView) myView.findViewById(R.id.listviewMoradores);
search = (EditText) myView.findViewById(R.id.search);
initList();
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.toString().equals("")){
initList();
}
else{
searchUser(charSequence.toString());
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return myView;
}
public void searchUser(String userToSearch) {
for(String user: users){
if(!user.contains(userToSearch)){
listUsers.remove(user);
}
}
adapter.notifyDataSetChanged();
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
private void initList(){
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
users = new String[objects.size()+1];
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
users[i] = objects.get(i).getUsername();
}
} else {
users[0] = "NONE";
}
}
});
listUsers = new ArrayList<>(Arrays.asList(users));
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.user, listUsers);
listView.setAdapter(adapter);
}
}
我几乎尝试了所有事情,但我一直遇到同样的错误,你们有什么想法我怎么能纠正这个?我是android编程的新手,所以欢迎任何帮助:)
P.S:英语不是我母语,对任何错误都很抱歉。答案 0 :(得分:0)
可能尝试使用AsyncTask。你可以获取列表,一旦完成,尝试绘制。可能列表没有填充列表并试图绘制它。