app wont start, here is the Logcat

2020-08-01 06:46发布

问题:

Process: com.josephvarkey996gmail.test1, PID: 3348
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.josephvarkey996gmail.test1/com.josephvarkey996gmail.test1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     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:304)
            at com.parse.ParseObject.getFromDisk(ParseObject.java:667)
            at com.parse.ParseUser$10.then(ParseUser.java:1056)
            at com.parse.ParseUser$10.then(ParseUser.java:1003)
            at bolts.Task$14.run(Task.java:796)
            at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
            at bolts.Task.completeAfterTask(Task.java:787)
            at bolts.Task.continueWithTask(Task.java:599)
            at bolts.Task.continueWithTask(Task.java:610)
            at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:1003)
            at com.parse.ParseUser.access$800(ParseUser.java:26)
            at com.parse.ParseUser$9.then(ParseUser.java:996)
            at com.parse.ParseUser$9.then(ParseUser.java:993)
            at com.parse.TaskQueue.enqueue(TaskQueue.java:61)
            at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:993)
            at com.parse.ParseUser.getCurrentUser(ParseUser.java:979)
            at com.parse.ParseUser.getCurrentUser(ParseUser.java:966)
            at com.josephvarkey996gmail.test1.MainActivity.onCreate(MainActivity.java:23)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

this is the mainactivity,im newbie in this i cant figure wats wrong with code.is it because of the parse object...................................................................................................................

package com.josephvarkey996gmail.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseUser;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ParseAnalytics.trackAppOpenedInBackground(getIntent( ));
        ParseUser currentUser = ParseUser.getCurrentUser();
        if(currentUser==null) {
            navigatetologin();
        }
        else
        {
            Log.i(TAG ,currentUser.getUsername());
        }
        // Enable Local Datastore.
        Parse.enableLocalDatastore(this);

        Parse.initialize(this, "XXXXXX", "YYYYYY");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int itemid = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (itemid == R.id.action_settings) {
           ParseUser.logOut();
            navigatetologin();
        }

        return super.onOptionsItemSelected(item);
    }

    private void navigatetologin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}

......................login activity............

package com.josephvarkey996gmail.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;


public class LoginActivity extends Activity {
    protected TextView mSignupTextview;
    protected EditText mUsername;
    protected EditText mPassword;
    protected Button nloginbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_login);
        mUsername = (EditText) findViewById(R.id.usernamefield);
        mPassword = (EditText) findViewById(R.id.passwordtextfield);
        nloginbutton = (Button) findViewById(R.id.loginbutton);
        nloginbutton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Retrieve the text entered from the EditText
                String usernametxt = mUsername.getText().toString();
                String password = mPassword.getText().toString();

                // Force user to fill up the form
                if (usernametxt.equals("") && password.equals("")) {
                    Toast.makeText(getApplicationContext(),
                            "log in to proceed",
                            Toast.LENGTH_LONG).show();

                } else {
                    // Save new user data into Parse.com Data Storage
                    setProgressBarIndeterminateVisibility(true);
                    ParseUser.logInInBackground(usernametxt, password, new LogInCallback() {
                        @Override
                        public void done(ParseUser parseUser, ParseException e) {
                             if (e==null) {
                                 Intent intent= new Intent(LoginActivity.this,MainActivity.class);
                                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                 startActivity(intent);
                             }
                            else {
                                 AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
                                 builder.setMessage(e.getMessage()).setTitle(R.string.signup_error_title).setPositiveButton(android.R.string.ok,null);
                                 AlertDialog dialog = builder.create();
                                 dialog.show();
                             }
                        }
                    });
                }

            }
        });




    mSignupTextview =(TextView)findViewById(R.id.signuptext);
                mSignupTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                intent = new Intent(LoginActivity.this, SignupActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

.................signup activity................

mport android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;


public class SignupActivity extends Activity {
    protected EditText mUsername;
    protected EditText mPassword;
    protected EditText mEmail;
    protected Button nbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_signup);
        mUsername = (EditText) findViewById(R.id.usernamefield);
        mPassword = (EditText) findViewById(R.id.passwordtextfield);
        mEmail = (EditText) findViewById(R.id.emailtextfield);
        nbutton = (Button) findViewById(R.id.signbutton);
        nbutton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Retrieve the text entered from the EditText
                String usernametxt = mUsername.getText().toString();
                String password = mPassword.getText().toString();
                String email= mEmail.getText().toString();
                // Force user to fill up the form
                if (usernametxt.equals("") && password.equals("")) {
                    Toast.makeText(getApplicationContext(),
                            "Please complete the sign up form",
                            Toast.LENGTH_LONG).show();

                } else {
                    // Save new user data into Parse.com Data Storage
                    setProgressBarIndeterminateVisibility(true);
                    ParseUser newUser =  new ParseUser();
                    newUser.setUsername(usernametxt);
                    newUser.setPassword(password);
                    newUser.setEmail(email);
                    newUser.signUpInBackground(new SignUpCallback() {
                        @Override
                        public void done(ParseException e) {
                            //SUCESS
                            if (e== null){
                                setProgressBarIndeterminateVisibility(false);
                                Intent intent= new Intent(SignupActivity.this,MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                            else {
                                AlertDialog.Builder builder=new AlertDialog.Builder(SignupActivity.this);
                                builder.setMessage(e.getMessage()).setTitle(R.string.signup_error_title).setPositiveButton(android.R.string.ok,null);
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                            }

                    });
                }

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_signup, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

回答1:

@Cumatru is right. You can't call Parse methods until it's initialized.

Rather than initializing Parse in an Activity, I recommend you to do so in an Application subclass:

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(Team.class);

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

Then add your Application subclass to AndroidManifest.xml:

<manifest>
    ...    
    <application
        android:name=".App"
        ...
        >
        <activity>
            ...
        </activity>
    </application>
</manifest>

It's a good idea to follow the Quick Start.



回答2:

Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXXXX", "YYYYYY");

Must be performed in this exact order before:

ParseUser currentUser = ParseUser.getCurrentUser();


回答3:

Simply, when you put code:

// Enable Local Datastore.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxx", "yyy");

into onCreate() of class which extends android.app.Application then don't forget to add line bellow into Manifest file:

 <application
    android:name=".NameOfYourApplicationClass"
    ...

because onCreate won't be called.



回答4:

You are trying to have operations on a null pointer:

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:304) at 

Something is wrong in line 304 of Parse.java?