java.lang.NullPointerException: Attempt to invoke

2020-08-14 02:57发布

问题:

Trying to load a class from Parse , however the app crashes as soon I launch it! Here is the code:

 ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_big_board);
    new RemoteDataTask().execute();

}

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(BigBoard.this);
        mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

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

    @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>(BigBoard.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 AdapterView.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(BigBoard.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);
            }
        });
    }
}

And this is what I obtain in the logcat :

> 10-22 20:40:18.705  12588-12872/obx.com.futurister E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
    Process: obx.com.futurister, PID: 12588
    java.lang.RuntimeException: An error occurred while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:309)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
            at com.parse.Parse.getParseDir(Parse.java:315)
            at com.parse.ParseCorePlugins.getCurrentUserController(ParseCorePlugins.java:129)
            at com.parse.ParseUser.getCurrentUserController(ParseUser.java:55)
            at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:888)
            at com.parse.ParseQuery.getUserAsync(ParseQuery.java:938)
            at com.parse.ParseQuery$3.call(ParseQuery.java:1196)
            at com.parse.ParseQuery$3.call(ParseQuery.java:1193)
            at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:1132)
            at com.parse.ParseQuery.findAsync(ParseQuery.java:1193)
            at com.parse.ParseQuery.findInBackground(ParseQuery.java:1161)
            at com.parse.ParseQuery.find(ParseQuery.java:981)
            at obx.com.futurister.BigBoard$RemoteDataTask.doInBackground(BigBoard.java:66)
            at obx.com.futurister.BigBoard$RemoteDataTask.doInBackground(BigBoard.java:44)
            at android.os.AsyncTask$2.call(AsyncTask.java:295)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
10-22 20:40:18.857  12588-12619/obx.com.futurister E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xb3a1fb90
10-22 20:40:18.955  12588-12619/obx.com.futurister E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xa96acbb0
10-22 20:40:18.996  12588-12588/obx.com.futurister E/WindowManager﹕ android.view.WindowLeaked: Activity obx.com.futurister.BigBoard has leaked window com.android.internal.policy.PhoneWindow$DecorView{a0f50b2 V.E...... R......D 0,0-1026,538} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:368)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
            at android.app.Dialog.show(Dialog.java:319)
            at obx.com.futurister.BigBoard$RemoteDataTask.onPreExecute(BigBoard.java:56)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604)
            at android.os.AsyncTask.execute(AsyncTask.java:551)
            at obx.com.futurister.BigBoard.onCreate(BigBoard.java:40)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

guess its the problem with the doInBackground() method? Am I missing something? Please help , Thanks

回答1:

Based on your comment I'm guessing you haven't initialized Parse. http://docs.parseplatform.org/android/guide/#local-datastore

   public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // Enable Local Datastore.
        Parse.enableLocalDatastore(this);

        // Register any ParseObject subclass. Must be done before calling Parse.initialize()
        ParseObject.registerSubclass(YourClass.class);

        Parse.initialize(this, "APPLICATION_ID", "CLIENT_KEY");    
    }
}

Make sure you add your App class to the manifest



回答2:

you need assign your App file in manifest Open your manifest file and add this code in manifest file in application tag, first line android:name=".App"

good luck in programming