我在名为(ParseInitialization.java)的类中进行了Parse初始化,该类扩展了Application并在运行我的应用程序(MainActivity.java)时遇到了RuntimeException 我在(MainActivity.java)的onCreate里面调用Parse.initialize我得到了一个NullpointException
这是我的代码:
MainActivity.java
package com.example.baharat2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuInflater;
import com.parse.ParseAnonymousUtils;
import com.parse.ParseUser;
public class MainActivity extends Activity {
private Button signin_btn ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signin_btn = (Button) findViewById(R.id.button1);
signin_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Determine whether the current user is an anonymous user
if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
// If user is anonymous, send the user to LoginSignupActivity.class
Intent intent = new Intent(MainActivity.this,LoginSignupActivity.class);
startActivity(intent);
finish();
} else {
// If current user is NOT anonymous user
// Get current user data from Parse.com
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// Send logged in users to Welcome.class
Intent intent = new Intent(MainActivity.this, Welcome.class);
startActivity(intent);
finish();
} else {
// Send user to LoginSignupActivity.class
Intent intent = new Intent(MainActivity.this,LoginSignupActivity.class);
startActivity(intent);
finish();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
ParseInitialization.java
package com.example.baharat2;
import android.app.Application;
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseObject;
import com.parse.ParseUser;
public class ParseInitialization extends Application {
@Override
public void onCreate() {
super.onCreate();
// Add your initialization code here
Parse.initialize(this, "xxx", "xxx");
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);
ParseObject testObject = new ParseObject("TestObject");
testObject.put("foo", "bar");
testObject.saveInBackground();
}
}
logcat的
03-21 11:05:56.133: E/AndroidRuntime(2348): FATAL EXCEPTION: main
03-21 11:05:56.133: E/AndroidRuntime(2348): Process: com.example.baharat2, PID: 2348
03-21 11:05:56.133: E/AndroidRuntime(2348): java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library.
03-21 11:05:56.133: E/AndroidRuntime(2348): at com.parse.ParseUser.checkApplicationContext(ParseUser.java:941)
03-21 11:05:56.133: E/AndroidRuntime(2348): at com.parse.ParseUser.getCurrentUser(ParseUser.java:813)
03-21 11:05:56.133: E/AndroidRuntime(2348): at com.example.baharat2.MainActivity$1.onClick(MainActivity.java:30)
03-21 11:05:56.133: E/AndroidRuntime(2348): at android.view.View.performClick(View.java:4438)
03-21 11:05:56.133: E/AndroidRuntime(2348): at android.view.View$PerformClick.run(View.java:18422)
03-21 11:05:56.133: E/AndroidRuntime(2348): at android.os.Handler.handleCallback(Handler.java:733)
03-21 11:05:56.133: E/AndroidRuntime(2348): at android.os.Handler.dispatchMessage(Handler.java:95)
03-21 11:05:56.133: E/AndroidRuntime(2348): at android.os.Looper.loop(Looper.java:136)
03-21 11:05:56.133: E/AndroidRuntime(2348): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-21 11:05:56.133: E/AndroidRuntime(2348): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:05:56.133: E/AndroidRuntime(2348): at java.lang.reflect.Method.invoke(Method.java:515)
03-21 11:05:56.133: E/AndroidRuntime(2348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-21 11:05:56.133: E/AndroidRuntime(2348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-21 11:05:56.133: E/AndroidRuntime(2348): at dalvik.system.NativeStart.main(Native Method)
03-21 11:06:01.753: I/Process(2348): Sending signal. PID: 2348 SIG: 9
此外,我在Manifest中有这个(ParseInitialization)活动
<activity
android:name="ParseInitialization">
</activity>
有什么建议吗?
答案 0 :(得分:3)
ParseInitialization
是一个应用程序,而不是一个活动。如果您想将其作为活动,请更改java代码。如果它是一个应用程序,那么你需要更改你的androidManifest
申请声明样本:
<application
android:name="com.example.baharat2.ParseInitialization" >
....
</application>