88660

Getting data from Parse.com using fragments

I am trying to pull data from parse.com and place the resulting in a ListView.

The code itself has no errors however when i run it, it crashes and i have no idea why.

LogCat suggests the bug is in the 'doInBackground' method however i have not been able to find a way to solve this.

i feel there is a simple solution to this.

thanks for any help :)

Fragment class:

public class fragname extends Fragment { // Declare Variables ListView listview; List<ParseObject> ob; ProgressDialog mProgressDialog; ArrayAdapter<String> adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // inflat and return the layout View v = inflater.inflate(R.layout.listview_main, container, false); new RemoteDataTask().execute(); return v; } private class RemoteDataTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); // Create a progressdialog mProgressDialog = new ProgressDialog(getActivity()); // Set progressdialog title mProgressDialog.setTitle("Loading Programme, Please Wait :)"); // Set progressdialog message mProgressDialog.setIndeterminate(false); // Show progressdialog mProgressDialog.show(); } @Override protected Void doInBackground(Void... params) { ParseQuery<ParseObject> query = new ParseQuery<ParseObject>( "Country"); query.orderByDescending("country"); try { ob = query.find(); } catch (com.parse.ParseException e) { Toast.makeText(getActivity(), "Error, " + e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } return null; }} protected void onPostExecute(Void result){ listview = (ListView) getView().findViewById(R.id.listview); adapter = new ArrayAdapter<String>(getActivity(), R.layout.listview_item); for (ParseObject country : ob) { adapter.add((String) country.get("country")); } listview.setAdapter(adapter); mProgressDialog.dismiss(); }

listview_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5sp" android:textSize="25sp" > </TextView>

LogCat:

12-03 16:13:30.380: D/dalvikvm(10654): GC_CONCURRENT freed 7646K, 53% free 6933K/14663K, paused 2ms+6ms 12-03 16:13:30.380: E/AndroidRuntime(10654): FATAL EXCEPTION: AsyncTask #1 12-03 16:13:30.380: E/AndroidRuntime(10654): java.lang.RuntimeException: An error occured while executing doInBackground() 12-03 16:13:30.380: E/AndroidRuntime(10654): at android.os.AsyncTask$3.done(AsyncTask.java:278) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 12-03 16:13:30.380: E/AndroidRuntime(10654): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.lang.Thread.run(Thread.java:856) 12-03 16:13:30.380: E/AndroidRuntime(10654): Caused by: java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library. 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseUser.checkApplicationContext(ParseUser.java:853) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseUser.getCurrentUser(ParseUser.java:726) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseUser.getCurrentSessionToken(ParseUser.java:756) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery.findFromNetworkAsync(ParseQuery.java:511) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery.access$4(ParseQuery.java:510) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery$5.runOnNetworkAsync(ParseQuery.java:455) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery.runCommandWithPolicyAsync(ParseQuery.java:397) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery.findWithCachePolicyAsync(ParseQuery.java:466) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery.findAsync(ParseQuery.java:449) 12-03 16:13:30.380: E/AndroidRuntime(10654): at com.parse.ParseQuery.find(ParseQuery.java:489) 12-03 16:13:30.380: E/AndroidRuntime(10654): at info.androidhive.slidingmenu.FindPeopleFragment$RemoteDataTask.doInBackground(FindPeopleFragme nt.java:66) 12-03 16:13:30.380: E/AndroidRuntime(10654): at info.androidhive.slidingmenu.FindPeopleFragment$RemoteDataTask.doInBackground(FindPeopleFragment.java:1) 12-03 16:13:30.380: E/AndroidRuntime(10654): at android.os.AsyncTask$2.call(AsyncTask.java:264) 12-03 16:13:30.380: E/AndroidRuntime(10654): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 12-03 16:13:30.380: E/AndroidRuntime(10654): ... 5 more 12-03 16:13:30.870: D/OpenGLRenderer(10654): Flushing caches (mode 0) 12-03 16:13:30.880: D/OpenGLRenderer(10654): Flushing caches (mode 0) 12-03 16:13:31.390: D/OpenGLRenderer(10654): Flushing caches (mode 1) 12-03 16:13:31.400: E/WindowManager(10654): Activity info.androidhive.slidingmenu.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41874b28 that was originally added here 12-03 16:13:31.400: E/WindowManager(10654): android.view.WindowLeaked: Activity info.androidhive.slidingmenu.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41874b28 that was originally added here 12-03 16:13:31.400: E/WindowManager(10654): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344) 12-03 16:13:31.400: E/WindowManager(10654): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 12-03 16:13:31.400: E/WindowManager(10654): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 12-03 16:13:31.400: E/WindowManager(10654): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 12-03 16:13:31.400: E/WindowManager(10654): at android.view.Window$LocalWindowManager.addView(Window.java:537) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.Dialog.show(Dialog.java:278) 12-03 16:13:31.400: E/WindowManager(10654): at info.androidhive.slidingmenu.FindPeopleFragment$RemoteDataTask.onPreExecute(FindPeopleFragment.java:53) 12-03 16:13:31.400: E/WindowManager(10654): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561) 12-03 16:13:31.400: E/WindowManager(10654): at android.os.AsyncTask.execute(AsyncTask.java:511) 12-03 16:13:31.400: E/WindowManager(10654): at info.androidhive.slidingmenu.FindPeopleFragment.onCreateView(FindPeopleFragment.java:35) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.BackStackRecord.run(BackStackRecord.java:622) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 12-03 16:13:31.400: E/WindowManager(10654): at android.os.Handler.handleCallback(Handler.java:605) 12-03 16:13:31.400: E/WindowManager(10654): at android.os.Handler.dispatchMessage(Handler.java:92) 12-03 16:13:31.400: E/WindowManager(10654): at android.os.Looper.loop(Looper.java:137) 12-03 16:13:31.400: E/WindowManager(10654): at android.app.ActivityThread.main(ActivityThread.java:4446) 12-03 16:13:31.400: E/WindowManager(10654): at java.lang.reflect.Method.invokeNative(Native Method) 12-03 16:13:31.400: E/WindowManager(10654): at java.lang.reflect.Method.invoke(Method.java:511) 12-03 16:13:31.400: E/WindowManager(10654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-03 16:13:31.400: E/WindowManager(10654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-03 16:13:31.400: E/WindowManager(10654): at dalvik.system.NativeStart.main(Native Method) 12-03 16:13:32.360: I/Process(10654): Sending signal. PID: 10654 SIG: 9

<strong>Edit</strong>

Parse Application class

public class ParseApplication extends Application { @Override public void onCreate() { super.onCreate(); // Add your initialization code here Parse.initialize(this, "**parse key**","**parse key**"); ParseUser.enableAutomaticUser(); ParseACL defaultACL = new ParseACL(); // If you would like all objects to be private by default, remove this // line. defaultACL.setPublicReadAccess(true); ParseACL.setDefaultACL(defaultACL, true); } }

Answer1:

You need to call Parse.initialize(context, oauthKey, oauthSecret) in your application before you make any queries to Parse. This tells the service which app is yours. You can find the keys on your Parse Dashboard.

This blog post might be helpful: http://www.michaelevans.org/blog/2013/08/14/tutorial-building-an-android-to-do-list-app-using-parse/

Recommend

  • Android language parsing difficulties
  • Google Map Undesirably Resets camera zoom out
  • Android Generic AsyncTask Class
  • how to do a GET request using retrofit2?
  • Volley library NetworkDispatcher.run error
  • Retrieve RSS feed and display it in text view
  • broadcast receiver inside activity
  • Android image didn't display to ImageView from image URL
  • Populating Spinner with JSON
  • Java AsyncTask passing variable to main thread
  • Android VideoView Quality is so bad on TV Box using Vitamio Library
  • Android: save file to downloads that can be viewed later
  • Turning String textfile into object array
  • Unable to retrieve number before incoming call in marshmallow
  • IllegalStateException “System services not available to Activities before onCreate()”
  • Retrofit 2 error: NetworkOnMainThreadException
  • Spring bean validation messages resolution
  • android.app.PendingIntent cannot be accessed ouside the package
  • FCM Data messages are not working properly
  • How to handle empty space in url when downloading image from web?
  • event.getSource() returns null Accessibility in android
  • Problem with rejecting incoming call
  • PDO error when wrong host name
  • How to copy styled text in JTextPane
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Volley JsonObjectRequest send headers in GET Request
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • Is there a mandatory requirement to switch app.yaml?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • AngularJs get employee from factory
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How can i traverse a binary tree from right to left in java?