Android Runtime and Nullpoint Exceptions

2020-04-21 05:22发布

问题:

I have Parse initialization in a class named (ParseInitialization.java) that extends Application and while running my application (MainActivity.java) I got a RuntimeException and whene I call Parse.initialize inside onCreate in (MainActivity.java) I got a NullpointException

here is my code:

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

Also I have this (ParseInitialization) activity in Manifest

 <activity
 android:name="ParseInitialization">
 </activity>

Any suggestions ?

回答1:

ParseInitialization is a application, not an activity. If you want it as an activity please change the java code. If it is an Application then you need to change your androidManifest

Sample of Application declaration:

 <application
        android:name="com.example.baharat2.ParseInitialization" >

....

</application>