打开下一个活动时崩溃的应用程序有问题。这是代码和android监视器。
package com.blabber.app.login;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.blabber.app.BlabberApplication;
import com.blabber.app.MyFirebaseMessagingService;
import com.blabber.app.R;
import com.blabber.app.activities.AddUserNameActivity;
import com.blabber.app.activities.ProfileActivity;
import com.blabber.app.business_details.BusinessDetailsActivity;
import com.blabber.app.data.ConnectionManager;
import com.blabber.app.data.model.User;
import com.blabber.app.login.LoginActivity;
import com.blabber.app.login.DaggerLoginComponent;
import com.blabber.app.login.LoginContract;
import com.blabber.app.login.LoginPresenterModule;
import com.blabber.app.splash.DaggerFacebookLoginComponent;
import com.blabber.app.utils.NavigationManager;
import com.blabber.app.utils.Utils;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.google.firebase.database.Transaction;
import com.google.firebase.iid.FirebaseInstanceId;
import org.json.JSONObject;
import java.util.Arrays;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.blabber.app.BlabberApplication;
import com.blabber.app.R;
import com.blabber.app.signup.SignUpActivity;
import com.blabber.app.utils.ActivityUtils;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.OnClick;
/**
* A login screen that offers login via email/password.
*/
public class LoginActivity extends AppCompatActivity implements LoginContract{
private static final String TYPE_FRIEND_REQUEST = "1";
private static final String TYPE_FRIEND_REQUEST_ACCEPTED = "2";
private static final String TYPE_FRIEND_REVIEW_TAG = "3";
private static final String TYPE_NEW_BUSINESS = "4";
@Inject
LoginPresenter mLoginPresenter;
LoginFragment mLoginFragment;
LoginContract.Presenter mPresenter;
@BindView(R.id.btn_fb_login)
Button mFacebookLoginButton;
@BindView(R.id.signin_tv)
TextView mSignInTextView;
private CallbackManager callbackManager;
public ProgressDialog progressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mLoginFragment = (LoginFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
if (mLoginFragment == null) {
mLoginFragment = LoginFragment.newInstance();
ActivityUtils.addFragmentToActivity(
getSupportFragmentManager(), mLoginFragment, R.id.contentFrame);
}
DaggerLoginComponent.builder()
.dataRepositoryComponent(((BlabberApplication) getApplication()).getUserDataRepositoryComponent())
.loginPresenterModule(new LoginPresenterModule(mLoginFragment))
.build()
.inject(this);
if (getIntent().getExtras() != null && getIntent().getExtras().getString("type") != null) {
String type = getIntent().getExtras().getString("type");
// E/MainActivity:: Key: google.sent_time Value: 1490611439936
// 03-27 12:44:16.082 27240-27240/? E/MainActivity:: Key: from Value: 723381489363
// 03-27 12:44:16.082 27240-27240/? E/MainActivity:: Key: type Value: 3
// 03-27 12:44:16.082 27240-27240/? E/MainActivity:: Key: review_id Value: 37
// 03-27 12:44:16.082 27240-27240/? E/MainActivity:: Key: google.message_id Value: 0:1490611439943980%923c630a923c630a
// 03-27 12:44:16.082 27240-27240/? E/MainActivity:: Key: business_id Value: 368
switch (type) {
case TYPE_FRIEND_REQUEST:
if (getIntent().getExtras().getString("friend_id") != null)
openFriendProfile(getIntent().getExtras().getString("friend_id"));
break;
case TYPE_FRIEND_REQUEST_ACCEPTED:
if (getIntent().getExtras().getString("friend_id") != null)
openFriendProfile(getIntent().getExtras().getString("friend_id"));
break;
case TYPE_FRIEND_REVIEW_TAG: {
Log.e(getClass().getSimpleName(), "in TYPE_FRIEND_REVIEW_TAG");
if (getIntent().getExtras().getString("business_id") != null) {
openBusiness(getIntent().getExtras().getString("business_id"));
Log.e(getClass().getSimpleName(), "in business_id: " + getIntent().getExtras().getString("business_id"));
}
}
break;
case TYPE_NEW_BUSINESS:
if (getIntent().getExtras().getString("business_id") != null)
openBusiness(getIntent().getExtras().getString("business_id"));
break;
}
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.e("MainActivity: ", "Key: " + key + " Value: " + value);
}
}
// }
else {
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage(getResources().getString(R.string.wait));
progressDialog.setCancelable(false);
DaggerLoginComponent.builder().dataRepositoryComponent(((BlabberApplication) getApplication()).getUserDataRepositoryComponent())
.loginPresenterModule(new LoginPresenterModule((LoginContract.View) this)).build().inject(this);
if (Utils.isLoggedBefore(this)) {
NavigationManager.goToMainActivity(LoginActivity.this);
} else {
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
progressDialog.show();
Profile.fetchProfileForCurrentAccessToken();
final String acesstoken = loginResult.getAccessToken().getToken();
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
//String user_name = (object.has("first_name") ? object.getString("first_name") : "") + "." + (object.has("last_name") ? object.getString("last_name") : "");
String user_name = object.has("name") ? object.getString("name") : "";
String user_image = "";
if (object.has("picture")) {
JSONObject ob = object.getJSONObject("picture");
if (ob != null) {
if (ob.has("data")) {
JSONObject ob2 = ob.getJSONObject("data");
user_image = ob2.has("url") ? ob2.getString("url") : "";
}
}
}
String token = FirebaseInstanceId.getInstance().getToken();
mPresenter.signInFaceBook(object.getString("id"), acesstoken, user_name, user_image, token);
} catch (Exception e) {
Log.e("LoginActivity", e.getMessage());
}
Log.v("LoginActivity", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture.width(350).height(350),email,first_name,last_name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
//NavigationManager.goToMainActivity(SplashActivity.this);
}
@Override
public void onError(FacebookException error) {
Log.e("facebook error", error.toString());
//NavigationManager.goToMainActivity(SplashActivity.this);
}
});
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
public void handleSuccessfulLogin(String username) {
progressDialog.hide();
//NavigationManager.goToMainActivity(SplashActivity.this);
if(username == null)
{
// NavigationManager.goToMainActivity(SplashActivity.this);
startActivity(new Intent(this, AddUserNameActivity.class));
}else{
if(username.equals("")) {
startActivity(new Intent(this, AddUserNameActivity.class));
}else{
NavigationManager.goToMainActivity(LoginActivity.this);
}
}
this.finish();
}
public void showError() {
progressDialog.hide();
Toast.makeText(this, "Login Error.", Toast.LENGTH_LONG).show();
}
public void setPresenter(LoginContract.Presenter presenter) {
mPresenter = presenter;
}
private void openFriendProfile( final String profile_id)
{
try {
BlabberApplication app = (BlabberApplication) getApplication();
Call<User> call = ConnectionManager.getApiManager().getProfile(app.getUserDataRepositoryComponent().getDataRepository().getUserId(),
app.getUserDataRepositoryComponent().getDataRepository().getAuthKey(), profile_id);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User profileResponse = response.body();
if (profileResponse != null) {
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
intent.putExtra("user_id", Integer.parseInt(profileResponse.getUserData().getId()));
intent.putExtra("profile_type", "id_profile");
//intent.putExtra("user_name", profileResponse.getUserData().getName());
//intent.putExtra("user_image", profileResponse.getUserData().getProfilePhoto());
startActivity(intent);
finish();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
}catch (Exception e){}
}
private void openBusiness(final String business_id)
{
Log.e(getClass().getSimpleName(),"in openBusiness with business_id: "+business_id);
Intent bintent = new Intent(com.blabber.app.login.LoginActivity.this, com.blabber.app.business_details.BusinessDetailsActivity.class);
bintent.putExtra("businessId", Integer.parseInt(business_id));
startActivity(bintent);
finish();
Log.e(getClass().getSimpleName(),"in openBusiness should be staarted");
}
}
错误
04-12 17:14:40.020 18930-18930/com.blabber.app E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabber.app/com.blabber.app.login.LoginActivity}: java.lang.ClassCastException: com.blabber.app.login.LoginActivity cannot be cast to com.blabber.app.login.LoginContract$View
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: com.blabber.app.login.LoginActivity cannot be cast to com.blabber.app.login.LoginContract$View
at com.blabber.app.login.LoginActivity.onCreate(LoginActivity.java:161)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
04-12 17:14:40.021 18930-18992/com.blabber.app W/GoogleTagManager: Tag Manager's event handler WILL NOT be installed (no container loaded)
04-12 17:14:40.021 18930-18992/com.blabber.app I/GoogleTagManager: Tag Manager initilization took 7ms
04-12 17:14:40.024 18930-18992/com.blabber.app D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=2018, _sc=SplashActivity, _si=-8379143149063394155}]
04-12 17:14:40.091 18930-18992/com.blabber.app D/FA: Logging event (FE): _ae, Bundle[{_o=crash, timestamp=1492010080020, fatal=1}]
04-12 17:14:40.242 18930-18930/com.blabber.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.blabber.app, PID: 18930
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabber.app/com.blabber.app.login.LoginActivity}: java.lang.ClassCastException: com.blabber.app.login.LoginActivity cannot be cast to com.blabber.app.login.LoginContract$View
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: com.blabber.app.login.LoginActivity cannot be cast to com.blabber.app.login.LoginContract$View
at com.blabber.app.login.LoginActivity.onCreate(LoginActivity.java:161)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
答案 0 :(得分:1)
发生此问题的原因是您尝试将LoginActivity强制转换为LoginContract,
以下是您进行投射的确切位置,
DaggerLoginComponent.builder().dataRepositoryComponent(((BlabberApplication) getApplication()).getUserDataRepositoryComponent())
.loginPresenterModule(new LoginPresenterModule((LoginContract.View) this))