3

Я пытался добавить тень на панель инструментов следуя этому примеру через здесь https://stackoverflow.com/a/26904102/4273056java.lang.ClassCastException: android.widget.LinearLayout не может быть приведен к android.support.v7.widget.Toolbar

Моя панель инструментов в деятельности

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
} 

Это XML-файл, в котором я добавил панель инструментов

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="top" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<android.support.v7.widget.Toolbar    
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/primaryColor" 
android:paddingTop="@dimen/app_bar_top_padding" 
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" 
app:theme="@style/MyCustomToolBarTheme"> 

</android.support.v7.widget.Toolbar> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- **** Place Your Content Here **** --> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:background="@drawable/shadow" /> 
</FrameLayout> 
</LinearLayout> 

Я получаю это в LogCat java.lang.ClassCastException: android.widget.LinearLayout не может быть добавлен в android.support.v7.widget.Toolbar

Как я могу избавиться от этой ошибки?

+0

Какая линия вы получаете эту ошибку? – Rohit5k2

+0

Опубликовать полную трассировку стека из logcat – Karakuri

ответ

5

Добавьте в свою панель инструментов атрибут id и проверьте, есть ли у вас другой элемент с тем же идентификатором.

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    ..... 
/> 

Также лучше:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    if (toolbar != null) 
     setSupportActionBar(toolbar); 
+0

@SourabhSNath удалить другой идентификатор. –

Смежные вопросы