我在尝试运行应用程序时遇到了一个出色的网络连接错误。我已经附上了下面的logcat来查看我收到的消息。
基本上,我正在尝试填充一个页面,该页面将显示解析数据库中有关用户的各种信息,例如姓名,年龄和个人资料图片。
08-18 15:14:58.973: E/AndroidRuntime(11753): FATAL EXCEPTION: main
08-18 15:14:58.973: E/AndroidRuntime(11753): Process: com.dooba.beta, PID: 11753
08-18 15:14:58.973: E/AndroidRuntime(11753): java.lang.RuntimeException: This query has an outstanding network connection. You have to wait until it's done.
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.parse.ParseQuery.checkIfRunning(ParseQuery.java:204)
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.parse.ParseQuery.findInBackground(ParseQuery.java:629)
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.dooba.beta.Fragment1.setConversationsList(Fragment1.java:148)
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.dooba.beta.Fragment1.onActivityCreated(Fragment1.java:71)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.View.measure(View.java:16497)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.View.measure(View.java:16497)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.View.measure(View.java:16497)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.View.measure(View.java:16497)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.View.measure(View.java:16497)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.Choreographer.doFrame(Choreographer.java:544)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.os.Handler.handleCallback(Handler.java:733)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.os.Handler.dispatchMessage(Handler.java:95)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.os.Looper.loop(Looper.java:136)
08-18 15:14:58.973: E/AndroidRuntime(11753): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-18 15:14:58.973: E/AndroidRuntime(11753): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 15:14:58.973: E/AndroidRuntime(11753): at java.lang.reflect.Method.invoke(Method.java:515)
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-18 15:14:58.973: E/AndroidRuntime(11753): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-18 15:14:58.973: E/AndroidRuntime(11753): at dalvik.system.NativeStart.main(Native Method)
08-18 15:15:06.893: I/Process(11753): Sending signal. PID: 11753 SIG: 9
以下是完整的代码
public class Fragment1 extends Fragment {
public interface Constants {
String LOG = "com.dooba.beta";
}
private String currentUserId;
private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ArrayList<String> age;
private ArrayList<String> headline;
private ArrayList<String> activityname;
private ArrayList<Images> alProfilePicture;
private ListView usersListView;
private Button logoutButton;
String userGender = ParseUser.getCurrentUser().getString("Gender");
String activityName = ParseUser.getCurrentUser().getString("ActivityName");
Number maxDistance = ParseUser.getCurrentUser().getNumber(
"Maximum_Distance");
String userLookingGender = ParseUser.getCurrentUser().getString(
"Looking_Gender");
Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
Number userage = ParseUser.getCurrentUser().getNumber("Age");
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setConversationsList();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container,
false);
return view;
}
private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
age = new ArrayList<String>();
headline = new ArrayList<String>();
activityname = new ArrayList<String>();
alProfilePicture = new ArrayList<Images>();
// String userActivitySelectionName = null;
ParseQuery<ParseUser> query = ParseUser.getQuery();
// query.whereEqualTo("ActivityName",userActivitySelectionName);
query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
.getObjectId());
// users with Gender = currentUser.Looking_Gender
query.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query.whereEqualTo("Looking_Gender", userGender);
query.setLimit(1);
query.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);
query.orderByDescending("Name");
Log.d(Constants.LOG, "");
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects,ParseException e) {
for(int i=0;i<objects.size();i++){
// Do whatever you need to extract object from "users"
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
ParseFile image = objects.get(i).getParseFile("ProfilePicture");
ParseImageView imageView = (ParseImageView) getView().findViewById(R.id.profilePictureResult);
// The placeholder will be used before and during the fetch, to be replaced by the fetched image
// data.
imageView.setPlaceholder(getResources().getDrawable(R.drawable.profile_pict));
imageView.setParseFile(image);
imageView.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
Log.i("ParseImageView",
"Fetched! Data length: " + data.length + ", or exception: " + e.getMessage());
}
});
}
}//for loop
});
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userlistname, ParseException e) {
if (e == null) {
for (int i = 0; i < userlistname.size(); i++) {
names.add(userlistname.get(i).get("Name").toString());
}
usersListView = (ListView) getActivity().findViewById(
R.id.userlistname);
namesArrayAdapter = new ArrayAdapter<String>(getActivity()
.getApplicationContext(), R.layout.user_list_item,
names);
usersListView.setAdapter(namesArrayAdapter);
usersListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a,
View v, int i, long l) {
openConversation(names, i);
}
});
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error loading user list", Toast.LENGTH_LONG)
.show();
}
}
});
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userlistheadline, ParseException e) {
if (e == null) {
for (int i = 0; i < userlistheadline.size(); i++) {
headline.add(userlistheadline.get(i).get("Headline")
.toString());
}
usersListView = (ListView) getActivity().findViewById(
R.id.userlistheadline);
namesArrayAdapter = new ArrayAdapter<String>(getActivity()
.getApplicationContext(), R.layout.user_list_all,
headline);
usersListView.setAdapter(namesArrayAdapter);
usersListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a,
View v, int i, long l) {
openConversation(names, i);
}
});
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error loading user list", Toast.LENGTH_LONG)
.show();
}
}
});
}
public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("Name", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, ParseException e) {
if (e == null) {
Intent intent = new Intent(getActivity()
.getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error finding that user", Toast.LENGTH_SHORT)
.show();
}
}
});
}
}
非常感谢任何帮助,因为我非常感激不尽。 在此先感谢您,如果您需要任何澄清,请告诉我。
答案 0 :(得分:2)
java.lang.RuntimeException:此查询具有出色的网络连接。你必须等到它完成。
听起来ParseQuery
不支持多个并发查找请求。setConversationsList()
重用相同的查询实例来执行3个后台请求。由于您没有更改每个请求的查询,因此您可以在一个FindCallback
中处理所有内容。
注意:将此方法分解为子方法将使代码更易于阅读和调试。
编辑:
每个处理程序都以查看list参数开始,并在每个对象上调用get()
,只是使用不同的参数。
for (int i = 0; i < users.size(); i++) {
//...
users.get(i).get("XXXX")
//...
}
我希望您能够在一次回拨中为每个不同的参数调用get()
。收集完所有信息后,即可更新用户界面。
for (ParseUser user : users) {
//...
user.get("AAA")
user.get("BBB")
user.get("CCC")
//...
}
您也可以使用for-each循环,而不是每次都调用users.get(i)
。