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>
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
}
};
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
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With