我想为内容主要添加标签!我的代码有问题!我有我的content_main XML代码!消息错误说:
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@+id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>
</TabHost>
并且有我的MainActivity代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabActivityLayout tabActivityLayout=new TabActivityLayout();
tabActivityLayout.setContentView(R.layout.content_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_share) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}&gt; 和TabActivityLayout代码:
public class TabActivityLayout extends TabActivity {
private TabHost mTabHost;
@Override
public void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(R.layout.content_main);
mTabHost =getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Francai sTab
intent = new Intent(this,FrancaisActivity.class);
spec =mTabHost.newTabSpec("Francais")
.setIndicator("Francais")
.setContent(intent);
// Anglais Tab
intent = new Intent(this,AnglaisActivity.class);
spec =mTabHost.newTabSpec("Anglais")
.setIndicator("Anglais")
.setContent(intent);
mTabHost.addTab(spec);
// Arabe Tab
intent = new Intent(this,ArabeActivity.class);
spec =mTabHost.newTabSpec("Arabe")
.setIndicator("Arabe")
.setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTab(2);
}
}
答案 0 :(得分:0)
以下代码抛出致命异常:
TabActivityLayout tabActivityLayout=new TabActivityLayout();
tabActivityLayout.setContentView(R.layout.content_main);
您不应该从其他活动设置contentView,setContentView()
将在活动onCreate()
方法中调用。
还有一个建议,以下行重复3次:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
如果您需要在onCreate()之外访问该视图,则需要将其声明为字段:
private DrawerLayout drawer;
然后设置初始化一次,就像你对其他视图一样setContentView()
之后:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);