我无法理解为什么我的应用程序无法接收推送通知,当我运行错误报告时,它说我无法在调用解析库之前初始化解析。我无法弄清楚我应该做些什么。请帮助。
来源
import com.parse.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.PushService;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private String un, pw;
public TextView username;
public EditText pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Parse.initialize(this, "QmmXYAxVVSoT7MGlE3ZcAseCoLehfZko3BZJbo1P",
"SQHTLKcpNKDCLZWTgfOOFPEcmFYkdmWywKNProWG");
PushService.setDefaultPushCallback(this, MainActivity.class);
PushService.startServiceIfRequired(this);
ParseInstallation.getCurrentInstallation().saveInBackground();
PushService.subscribe(this, "push", MainActivity.class);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.editloginusername);
pass = (EditText) findViewById(R.id.editpass);
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
un = username.getText().toString();
pw = pass.getText().toString();
ParseUser.logInInBackground(un, pw, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
new CallMainPage().execute();
} else {
// username.setText("");
pass.setText("");
makeToast("User name or password not recognized.");
}
}
});
}
});
Button signup = (Button) findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new CallSignup().execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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 id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class CallSignup extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... urls) {
Intent intent = new Intent(MainActivity.this, Signup.class);
MainActivity.this.startActivity(intent);
return "";
}
}
public class CallMainPage extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... urls) {
Intent intent = new Intent(MainActivity.this, MainPage.class);
MainActivity.this.startActivity(intent);
return "";
}
}
public void makeToast(String text) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
答案 0 :(得分:0)
你解决了问题吗?如果没有,您可以关注official tutorial。设置过程的大部分内容都是在Application类中完成的。
也许这就是问题所在,您在MainActivity中调用了 Parse.initialize , setDefaultPushCallback ,而不是在Application类中。