trna ha minh
trna ha minh

Reputation: 253

ClassCastException android

This is my Main App

public class MainApp extends Application {
    private MainComponent component;
    private static Context context;

    public MainApp() {
        this.component = initDagger();
        context = getApplicationContext();
    }

    public MainComponent getComponent() {
        return component;
    }

    public static Context getContext() {
        return context;
    }

    private MainComponent initDagger() {
        return DaggerMainComponent.builder().build();
    }
}

In main activity I use these codes below and receive an error

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        unbinder = ButterKnife.bind(this);

        ((MainApp) getApplication()).getComponent().inject(this);

//        presenter = new MainActivityPresenter();
        presenter.attachView(this);
    }

This is the error that I received. It said that I cannot cast Application to MainApp. But I follow dagger example and I see that they do this and it works. What is the problem with my code

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.huytrinh.sampledagger.MainApp
                                                                                   at com.example.huytrinh.sampledagger.MainActivity.onCreate(MainActivity.java:39)
                                                                                   at android.app.Activity.performCreate(Activity.java:5360)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)

Upvotes: 0

Views: 262

Answers (3)

KeLiuyue
KeLiuyue

Reputation: 8237

Try this.

First

To get MainApp

private static MainApp instance;

public
static MainApp getInstance() {
    return instance;
}

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

Second

MainApp.getInstance().getComponent().inject(this);

Edit

<application
    android:name=".MainApp"
    ... />

Upvotes: 0

Sharath kumar
Sharath kumar

Reputation: 4132

1.Your are getting exception because you dint include the custom application class in manifest.Add the class in manifest like this.

<application
    android:name=".MainApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"

2.Create dagger class and other things only after application is created i.e in oncreate of the application class

@Override
public void onCreate() {
    super.onCreate();
    this.component = initDagger();
    context = getApplicationContext();
}

Upvotes: 0

Lalit Jadav
Lalit Jadav

Reputation: 1427

In Manifest add your name to application tag

<application
        android:name=".MainApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

Upvotes: 1

Related Questions