Activity is not assigned to Android.app.Activity Manifest XML

16,894

Solution 1

Firstly, you only declare activities in manifest file, not fragments. So remove this line from manifest file

<activity
        android:name=".FeedbackMain"
        android:label="@string/title_activity_feedback_main" >
    </activity>

Secondly, if you want to use actionbar, you class should extend ActionBarActivity not FragmentActivity. so change your mainactivity from

public class MainActivity extends FragmentActivity {

to

public class MainActivity extends ActionBarActivity{

then make sure you call getSupportActionBar() if you are using support library

EDIT:

ActionBar.TabListener tabListener = new ActionBar.TabListener() { 
@Override 
public void onTabReselected( 
android.support.v7.app.ActionBar.Tab arg0, 
FragmentTransaction arg1) { 
// TODO Auto-generated method stub 

} 

@Override 
public void onTabSelected( 
android.support.v7.app.ActionBar.Tab arg0, 
FragmentTransaction arg1) { 
// TODO Auto-generated method stub 

} 

@Override 
public void onTabUnselected( 
android.support.v7.app.ActionBar.Tab arg0, 
FragmentTransaction arg1) { 
// TODO Auto-generated method stub 

} 
};

Solution 2

this error is caused because FeedBackMain is declared as Activity not as a Fragment

 <activity
        android:name=".FeedbackMain"
        android:label="@string/title_activity_feedback_main" >
    </activity>

if you really want use it as Activity Change

public class FeedbackMain extends Fragment {

to

public class FeedbackMain extends FragmentActivity {

but if you want use it as Fragment just delete the declaration inside your AndroidManifest.xml

use actionBar = getSupportActionBar(); instead of getActionBar()

You need some method implementations:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {

    public void onTabReselected(android.app.ActionBar.Tab tab,
                                android.app.FragmentTransaction ft) {
    }

    public void onTabSelected(android.app.ActionBar.Tab tab,
                              android.app.FragmentTransaction ft) {
        // on tab selected show respected fragment view
        Tab.setCurrentItem(tab.getPosition());
    }


    public void onTabUnselected(android.app.ActionBar.Tab tab,
                                android.app.FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(
            android.support.v7.app.ActionBar.Tab arg0,
            FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(
            android.support.v7.app.ActionBar.Tab arg0,
            FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(
            android.support.v7.app.ActionBar.Tab arg0,
            FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
};
Share:
16,894
QWERTY
Author by

QWERTY

Updated on June 09, 2022

Comments

  • QWERTY
    QWERTY almost 2 years

    I am having some trouble with the fragment in Android Studio. So basically this is my MainActivity:

    public class MainActivity extends FragmentActivity {
    final Context context = this;
    
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager) findViewById(R.id.pager);
        Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar = getActionBar();
                actionBar.setSelectedNavigationItem(position);
            }
        });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();
        // Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                                        android.app.FragmentTransaction ft) {
            }
    
            @Override
            public void onTabSelected(android.app.ActionBar.Tab tab,
                                      android.app.FragmentTransaction ft) {
                // on tab selected show respected fragment view
                Tab.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                                        android.app.FragmentTransaction ft) {
            }
        };
    
        // Add New Tab
        actionBar.addTab(actionBar.newTab().setText("Feedback")
                .setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("History Log")
                .setTabListener(tabListener));
    
    
    }
    }
    

    And then my another class which is one of the tab code:

    public class FeedbackMain extends Fragment {
    View feedbackMainView;
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        feedbackMainView = inflater.inflate(R.layout.activity_feedback_main,
                container, false);
    
        return feedbackMainView;
    }
    }
    

    And then my Manifest:

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".FeedbackMain"
            android:label="@string/title_activity_feedback_main" >
        </activity>
    

    However, I am getting red lines under .FeedbackMain and the error message is:

    .FeedbackMain is not assignable to 'android.app.Activity'
    

    I was wondering why is it so? It was working fine with Android development tools but then when I switched to Android Studio, this happens.

    Thanks in advance.

    Edit

    xml file for mainActivity:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    

    style.xml:

    <resources>
    
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
    
    </resources>
    
  • QWERTY
    QWERTY almost 9 years
    But when I removed it, it tells me null pointer exception at this line: actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  • QWERTY
    QWERTY almost 9 years
    But when I removed it, it tells me null pointer exception at this line: actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  • QWERTY
    QWERTY almost 9 years
    I tried to changed it just now but it still crashed
  • AlexWalterbos
    AlexWalterbos almost 9 years
    So actionBar is null, which means getActionBar() returns null. Can you add the layout xml file of your fragment and activity to your post?
  • Jorgesys
    Jorgesys almost 9 years
    ok Denise you want to use it as a fragment delete the declaration inside the AndroidManifest.xml and keep extending as a Fragment.
  • QWERTY
    QWERTY almost 9 years
    Yeah I deleted already but then it gives me an error message of null pointer exception at actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  • Bidhan
    Bidhan almost 9 years
    Add getWindow().requestFeature(Window.FEATURE_ACTION_BAR); before setContentView(R.layout.activity_main) inside your MainActivity.java and see if it works.
  • QWERTY
    QWERTY almost 9 years
    I edited the post already. Basically the fragment layout is just linear layout with a lot of textview
  • AlexWalterbos
    AlexWalterbos almost 9 years
    Checkout this tutorial on setting up the ActionBar. Your activity need to extend ActionBarActivity, for example. This tutorial (and the next steps) should help you out.
  • Bidhan
    Bidhan almost 9 years
    Alright, go inside your res>values folder and post the styles.xml file here.
  • Jorgesys
    Jorgesys almost 9 years
    thats another issue, in MainActivity add import android.support.v7.app.ActionBarActivity; and extend from ActionBarActivity : public class MainActivity extends ActionBarActivity { use "actionBar = getSupportActionBar();" instead of getActionBar()
  • QWERTY
    QWERTY almost 9 years
    After I changed it there is another error message at the ActionBar.TabListener. The error message as Class anonymous class derived from TabListener must either be declared abstrct
  • QWERTY
    QWERTY almost 9 years
    Hey any ideas? I am still stuck
  • QWERTY
    QWERTY almost 9 years
    After I changed it there is another error message at the ActionBar.TabListener. The error message as Class anonymous class derived from TabListener must either be declared abstract or implement abstract method onTabSelected
  • QWERTY
    QWERTY almost 9 years
    Alright but is there any other way to fix this if I wanted to stick with my code?
  • QWERTY
    QWERTY almost 9 years
    I have to remove the Override as there are some red line highlighted underneath
  • AlexWalterbos
    AlexWalterbos almost 9 years
    You have to remove the ActionBar then.
  • Durga Mohan
    Durga Mohan almost 9 years
    are you using supprort library?
  • QWERTY
    QWERTY almost 9 years
    Sorry but how do I check? Is it the one under gradle? dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' }
  • QWERTY
    QWERTY almost 9 years
    But I thought without the action bar, there will be no title for the tab?
  • Durga Mohan
    Durga Mohan almost 9 years
    yes, did you import these in your activity? import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab;
  • QWERTY
    QWERTY almost 9 years
    Yeah these are my imports: import android.support.v7.app.ActionBar; import android.content.Context; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar.Tab;
  • Durga Mohan
    Durga Mohan almost 9 years
    also this one import android.support.v7.app.ActionBar.TabListener;
  • QWERTY
    QWERTY almost 9 years
    Nope, the same error message still there. Any ideas?
  • Jorgesys
    Jorgesys almost 9 years
    I can help you Denise, but i think you have more than one issue do you have a link for the complete code?
  • QWERTY
    QWERTY almost 9 years
  • Durga Mohan
    Durga Mohan almost 9 years
    Well, i got the error, in you code change android.app.ActionBar.Tab to just Tab like public void onTabReselected(android.app.ActionBar.Tab tab, to public void onTabReselected(Tab tab,
  • Durga Mohan
    Durga Mohan almost 9 years
  • Jorgesys
    Jorgesys almost 9 years
    Well I have added the methods that you need to implement.