我正在使用标签菜单。因此,我创建了Fragment
页和适配器。当我添加片段页以显示时说“无法分配给android.app.activity”。因此我切换到{{ 1}}从FragmentActivity
,但这次我得到了新的错误 “不能被转换为Fragment
”。
我该怎么办。我真的很累。我还清理了项目和缓存,但是没有用。
Fragment
Testtab1与tab2相同的代码,只是名称不同。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tabdene, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
testtab1 tab1 = new testtab1();
return tab1;
case 1:
testtab2 tab2 = new testtab2();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position){
switch (position) {
case 0:
return "FREE";
case 1:
return "PAID";
}
return null;
}
}
}
清单
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class testtab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View pView = inflater.inflate(R.layout.activity_testtab1, container ,false);
return pView;
}
}
<?xml version="1.0" encoding="utf-8"?>