問題一:
在Android開發(fā)中,使用ToolBar控件替代ActionBar控件,需要在java代碼中使用setSupportActionBar()方法,如下:
Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolBar);
this.setSupportActionBar(toolbar);
原因:方法參數報錯
這種報錯是因為導錯了類,把以下代碼
import android.widget.Toolbar;
更換成以下代碼
import android.support.v7.widget.Toolbar;
修改ok
問題二:
carsh報如下錯誤:Unable to start activity ComponentInfo{com.xiaoyan.wether/com.xiaoyan.wether.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
最終找到原因:
在AndoridMainfest.xml中如下設置:
android:theme="@style/AppTheme"
然后sytle中AppTheme如下設置:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
其中parent="Theme.AppCompat.Light.DarkActionBar"設置為parent="Theme.AppCompat.Light.NoActionBar"即可。
ok。