Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity is not assignable to Activity

Tags:

java

android

I have read several posts for the last couple of days and i can not seem to figure out what my issue is. I have checked the Activity names and they are the same. I have also checked my manifest and I have the . in front of the activities as well.

Here is my logcat file:

02-23 16:48:22.438    1508-1508/com.pctoolman.planme.app E/AndroidRuntime﹕ FATAL   
         EXCEPTION: main
         java.lang.RuntimeException: Unable to instantiate activity     
         ComponentInfo{com.pctoolman.planme.app/com.pctoolman.planme.app.PlanMeMainActivity}:
         java.lang.ClassCastException: com.pctoolman.planme.app.PlanMeMainActivity cannot be 
         cast 
         to android.app.Activity
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
         at android.app.ActivityThread.access$600(ActivityThread.java:141)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:5103)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:525)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.ClassCastException: com.pctoolman.planme.app.PlanMeMainActivity 
         cannot
         be cast to android.app.Activity
         at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
         at android.app.ActivityThread.access$600(ActivityThread.java:141)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:5103)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:525)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         at dalvik.system.NativeStart.main(Native Method)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pctoolman.planme.app" >
        <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
        <activity
                android:name="com.pctoolman.planme.app.PlanMeMainActivity"
                android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
            android:name="com.pctoolman.planme.app.NewEventSetupActivity"
            android:label="@string/setup">
    </activity>
</application> 

PlanMeMainFragment.java

package com.pctoolman.planme.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class PlanMeMainFragment extends Fragment {

private Button mNewButton, mExistingButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.planme_main_fragment, parent, false);

        mNewButton = (Button)v.findViewById(R.id.new_event_button);
        mNewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(getActivity(), NewEventSetupActivity.class);
                getActivity().startActivity(myIntent);
            }
        });
        return  v;
    }
}

PlanMeMainActivity.java

package com.pctoolman.planme.app;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class PlanMeMainActivity extends Fragment {

    public Fragment createFragment() {
        return new PlanMeMainFragment();
    }

}
like image 303
hozdaman Avatar asked Feb 23 '14 22:02

hozdaman


4 Answers

Change this line

public class PlanMeMainActivity extends Fragment {

to

public class PlanMeMainActivity extends FragmentActivity {

Here you can find all that you need to know about Activities and even more. Cheers

like image 89
Yakiv Mospan Avatar answered Oct 02 '22 22:10

Yakiv Mospan


Where are you wrong?

  <activity
          android:name="com.pctoolman.planme.app.PlanMeMainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Why are you wrong?

The mistake is that you added the Fragment in the Manifest as Activity. However Fragments are not activities

What you should do?

You should add the fragment in an activity and then define that activity in the Manifest

like image 32
Quamber Ali Avatar answered Oct 02 '22 23:10

Quamber Ali


Your PlanMeMainActivity isn't an Activity; it's a Fragment. Your activity needs to be an activity, and you can then add fragments to it.

like image 26
chrylis -cautiouslyoptimistic- Avatar answered Oct 02 '22 22:10

chrylis -cautiouslyoptimistic-


Clean Project solved this error. The root cause of the error in my case was different, but I got the same "Activity is not assignable to Activity" error.

I faced this problem after copying existing (perfectly working) Android Project to create a new one. After copying the project, the AppCompatActivity was not recognized causing all the activities in the Android Manifest file to show the error "activity-is-not-assignable-to-activity".
Which was solved by cleaning the project.

like image 2
Lalit Rane Avatar answered Oct 03 '22 00:10

Lalit Rane