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)
这是主要的活动,即时通讯新手在这个我无法想象代码中的错误。它是因为解析对象....................... .................................................. ..........................................
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);
}
}
......................登录活动............
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);
}
}
.................注册活动................
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);
}
}
答案 0 :(得分:6)
@Cumatru是对的。您无法调用Parse方法,直到它被初始化。
我建议您在Activity
子类中执行此操作,而不是在Application
中初始化Parse:
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");
}
}
然后将您的Application
子类添加到AndroidManifest.xml
:
<manifest>
...
<application
android:name=".App"
...
>
<activity>
...
</activity>
</application>
</manifest>
关注Quick Start。
是一个好主意答案 1 :(得分:4)
new Date().valueOf()
new Date().getTime()
Date.parse(new Date())
new Date() * 1
必须按照以下确切顺序执行:
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXXXX", "YYYYYY");
答案 2 :(得分:3)
简单地说,当你输入代码时:
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxx", "yyy");
进入扩展android.app.Application的类的onCreate(),然后不要忘记将文件添加到清单文件中:
<application
android:name=".NameOfYourApplicationClass"
...
因为onCreate
不会被调用。
答案 3 :(得分:0)
您正尝试对空指针进行操作:
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
Parse.java的第304行出了什么问题?