doinbackground方法

时间:2015-11-04 10:21:18

标签: android android-studio

我正在测试一个应用程序,用于从parse.com检索我的listview数据 我的代码中没有错误,但在运行应用程序时关闭说"不幸的是应用程序停止了#34; 这是我的日志猫

FATAL EXCEPTION: AsyncTask #1
11-02 17:14:54.050 13134 13185 E   AndroidRuntime Process: com.mycompany.myapp2, PID: 13134


11-02 17:14:54.050 13134 13185 E   AndroidRuntime   java.lang.RuntimeException: An erroroccured while executing doInBackground()


11-02 17:14:54.050 13134 13185 E   AndroidRuntime. at android.os.AsyncTask$3.done(AsyncTask.java:304)

我的doinbackground方法

private class RemoteDataTask extends AsyncTask<Void, Void, Void>
{

    @Override
    protected Void doInBackground(Void[] p1)
    {

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("country");
        query.addDescendingOrder("_created_at");
        try{
            ob = query.find();
        }catch (ParseException e) {
            Log.e("error", e.getMessage());
            e.printStackTrace();


        }
        return null;

    }

我使用此链接作为我的代码 Link

** parseapplication.java

package com.mycompany.myapp;
import android.app.*;
Import com.parse.*;

public class ParseApplication extends Application
{

@Override
public void onCreate()
{
    // TODO: Implement this method
    super.onCreate();

    Parse.initialize(this, "9MoehuvnUJRKcvF8SL3nbW0slHHJNPJU4ONXGcD5", "kmBoHM4iwdm0YiyeR7aoMlyMgC5oh1oyTIOt34iu");

    ParseUser.enableAutomaticUser();
    ParseACL defaultACL= new ParseACL();

    defaultACL.setPublicReadAccess(true);
    ParseACL.setDefaultACL(defaultACL, true);
}

}

main activity.java

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.util.*;
import android.widget.*;
import com.parse.*;
import java.util.*;
import android.widget.AdapterView.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity 
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listview_main);

    new RemoteDataTask().execute();

}

private class RemoteDataTask extends AsyncTask<Void, Void, Void>
{

    @Override
    protected Void doInBackground(Void[] p1)
    {

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("country");
        query.addDescendingOrder("_created_at");
        try{
            ob = query.find();
        }catch (ParseException e) {
            Log.e("error", e.getMessage());
            e.printStackTrace();


        }
        return null;

    }


    @Override
    protected void onPreExecute()
    {
        // TODO: Implement this method
        super.onPreExecute();

        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("parsetesting");
        mProgressDialog.setMessage("LOADING PLEASE WAIT");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into an ArrayAdapter
        adapter = new ArrayAdapter<String>(MainActivity.this,
                                           R.layout.listview_item);
        // Retrieve object "name" from Parse.com database
        for (ParseObject country : ob) {
            adapter.add((String) country.get("name"));
        }
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
        // Capture button clicks on ListView items
        listview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // Send single item click data to SingleItemView Class
                    Intent i = new Intent(MainActivity.this,
                                          SingleItemView.class);
                    // Pass data "name" followed by the position
                    i.putExtra("name", ob.get(position).getString("name")
                               .toString());
                    // Open SingleItemView.java Activity
                    startActivity(i);
                }
            });
    }






}

}

是的,得到了​​解决方案。正如vishwajeet Singh所说。删除应用程序java做的并在setcontentview下面的主要活动的增加方法中添加parse.initialize(这,你我,你的密钥)? 谢谢vishwajeet Singh

**

4 个答案:

答案 0 :(得分:1)

编辑: 在MainActivity.java中更改onCreate方法

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listview_main);
Parse.initialize(this, "9MoehuvnUJRKcvF8SL3nbW0slHHJNPJU4ONXGcD5", "kmBoHM4iwdm0YiyeR7aoMlyMgC5oh1oyTIOt34iu");

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

    new RemoteDataTask().execute();

}

这是错误:

  

E / AndroidRuntime(558):引起:java.lang.RuntimeException:你必须   在使用之前调用Parse.initialize(context,oauthKey,oauthSecret)   解析库。

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

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

答案 1 :(得分:0)

您忘了将应用程序类添加到AndroidManifest.xml

<application name="com.mycompany.myapp.ParseApplication"

您还应该删除Parse.initialize()中的Activity方法调用,并将其添加回您的应用程序类。

答案 2 :(得分:-1)

看看国家的拼写中有拼写错误

    @Override
    protected Void doInBackground(Void... params) {
        // Locate the class table named "Country" in Parse.com
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "Country");
        query.orderByDescending("_created_at");
        try {
            ob = query.find();
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

答案 3 :(得分:-1)

在阅读您的logcat日志时,似乎这是您的实际错误:

java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library.

所述
08-27 15:30:33.610: E/AndroidRuntime(558): FATAL EXCEPTION: AsyncTask #1
08-27 15:30:33.610: E/AndroidRuntime(558): java.lang.RuntimeException: An error occured while executing doInBackground()
08-27 15:30:33.610: E/AndroidRuntime(558):      at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-27 15:30:33.610: E/AndroidRuntime(558):      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.lang.Thread.run(Thread.java:856)
08-27 15:30:33.610: E/AndroidRuntime(558): Caused by: java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library.
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseUser.checkApplicationContext(ParseUser.java:853)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseUser.getCurrentUser(ParseUser.java:726)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseUser.getCurrentSessionToken(ParseUser.java:756)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery.findFromNetworkAsync(ParseQuery.java:520)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery.access$200(ParseQuery.java:77)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery$4.runOnNetworkAsync(ParseQuery.java:436)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery.runCommandWithPolicyAsync(ParseQuery.java:383)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery.findWithCachePolicyAsync(ParseQuery.java:447)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery$6.call(ParseQuery.java:492)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery$6.call(ParseQuery.java:490)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:643)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.parse.ParseQuery.find(ParseQuery.java:490)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.example.parselistview.MainActivity$RemoteDataTask.doInBackground(MainActivity.java:51)
08-27 15:30:33.610: E/AndroidRuntime(558):      at com.example.parselistview.MainActivity$RemoteDataTask.doInBackground(MainActivity.java:1)
08-27 15:30:33.610: E/AndroidRuntime(558):      at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-27 15:30:33.610: E/AndroidRuntime(558):      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-27 15:30:33.610: E/AndroidRuntime(558):      ... 5 more

始终建议从上到下检查错误日志,首先修复它报告的第一个错误。至于这个特定的,请看看https://parse.com/docs/android/guide#getting-started