Activity is not assigned to Android.app.Activity Manifest XML
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
}
};
QWERTY
Updated on June 09, 2022Comments
-
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 almost 9 yearsBut when I removed it, it tells me null pointer exception at this line: actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
-
QWERTY almost 9 yearsBut when I removed it, it tells me null pointer exception at this line: actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
-
QWERTY almost 9 yearsI tried to changed it just now but it still crashed
-
AlexWalterbos almost 9 yearsSo
actionBar
is null, which meansgetActionBar()
returns null. Can you add the layout xml file of your fragment and activity to your post? -
Jorgesys almost 9 yearsok Denise you want to use it as a fragment delete the declaration inside the AndroidManifest.xml and keep extending as a Fragment.
-
QWERTY almost 9 yearsYeah I deleted already but then it gives me an error message of null pointer exception at actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
-
Bidhan almost 9 yearsAdd
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
beforesetContentView(R.layout.activity_main)
inside your MainActivity.java and see if it works. -
QWERTY almost 9 yearsI edited the post already. Basically the fragment layout is just linear layout with a lot of textview
-
AlexWalterbos almost 9 yearsCheckout this tutorial on setting up the
ActionBar
. Your activity need to extendActionBarActivity
, for example. This tutorial (and the next steps) should help you out. -
Bidhan almost 9 yearsAlright, go inside your res>values folder and post the styles.xml file here.
-
Jorgesys almost 9 yearsthats 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 almost 9 yearsAfter 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 almost 9 yearsHey any ideas? I am still stuck
-
QWERTY almost 9 yearsAfter 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 almost 9 yearsAlright but is there any other way to fix this if I wanted to stick with my code?
-
QWERTY almost 9 yearsI have to remove the Override as there are some red line highlighted underneath
-
AlexWalterbos almost 9 yearsYou have to remove the
ActionBar
then. -
Durga Mohan almost 9 yearsare you using supprort library?
-
QWERTY almost 9 yearsSorry 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 almost 9 yearsBut I thought without the action bar, there will be no title for the tab?
-
Durga Mohan almost 9 yearsyes, did you import these in your activity? import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab;
-
QWERTY almost 9 yearsYeah 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 almost 9 yearsalso this one import android.support.v7.app.ActionBar.TabListener;
-
QWERTY almost 9 yearsNope, the same error message still there. Any ideas?
-
Jorgesys almost 9 yearsI can help you Denise, but i think you have more than one issue do you have a link for the complete code?
-
QWERTY almost 9 yearsLet us continue this discussion in chat.
-
Durga Mohan almost 9 yearsWell, 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 almost 9 yearsLet us continue this discussion in chat.
-
Jorgesys almost 9 yearsWell I have added the methods that you need to implement.