为什么加上Toolbar就闪退(java)

凡心-不葬 2016-02-08 07:07:39
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shouye);

// 正页
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
toolbar.setTitle("酒店登陆系统");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

shouye.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@android:color/white" />

</android.support.design.widget.CoordinatorLayout>
...全文
406 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
YSoup 2019-10-16
  • 打赏
  • 举报
回复
你导错包了吧。要这样导包import android.support.v7.widget.Toolbar; 而不是import android.widget.Toolbar;
李元静 2016-02-10
  • 打赏
  • 举报
回复
你创建项目可能选的默认创建有标题栏的项目,因为主题有标题栏,你在设置标题栏就会有冲突,所以要将res/value/style.xml文件的AppTheme主题的父类改成NoActionBar
李元静 2016-02-10
  • 打赏
  • 举报
回复
把你的style文件的apptheme改成notitltbar
laymenISmouse 2016-02-08
  • 打赏
  • 举报
回复

建议你去查看打印的log报什么错误,这样就好定位
laymenISmouse 2016-02-08
  • 打赏
  • 举报
回复
同学你好,你的代码没有问题,我测试过了

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧