使用片段从Parse.com获取数据

时间:2013-12-03 16:35:51

标签: android listview fragment parse-platform

我正在尝试从parse.com提取数据并将结果放在ListView中。

代码本身没有错误但是当我运行它时,它崩溃了,我不明白为什么。

LogCat建议错误在'doInBackground'方法中,但我无法找到解决此问题的方法。

我觉得有一个简单的解决方案。

感谢您的帮助:))

片段类:

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

修改

解析应用程序类

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

}

1 个答案:

答案 0 :(得分:1)

在对Parse进行任何查询之前,您需要在应用程序中调用Parse.initialize(context,oauthKey,oauthSecret)。这告诉服务哪个应用程序是你的。您可以在Parse仪表板上找到密钥。

此博客文章可能会有所帮助:http://www.michaelevans.org/blog/2013/08/14/tutorial-building-an-android-to-do-list-app-using-parse/