2016-01-24 72 views
1

所以gradle這個構建就成功,但我的應用程序正在強制關閉,由於行:Caused by: java.lang.ClassCastException: android.support.design.widget.AppBarLayout cannot be cast to android.support.design.widget.CoordinatorLayout如何在Android Studio中將AppBarLayout轉換爲CoordinatorLayout?

我試圖從activity_home.xml刪除AppBarLayout標籤:

<android.support.design.widget.AppBarLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    android:id="@+id/home_Layout"> 

和思想要移動它並在佈局目錄中創建一個單獨的App_Bar.xml,但關於CoordinatorLayout強制轉換爲AppBarLayout的錯誤仍然存​​在,任何人都可以幫助我找到背後的真正問題?

這裏是我的HomeActivity.java(MainActivity.java)

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    homeLayout = (CoordinatorLayout) findViewById(R.id.home_Layout); 
    setSupportActionBar(toolbar); 




    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(); 
     } 
    }); 
    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 

@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_home, 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. 
    switch (item.getItemId()){ 

    //noinspection SimplifiableIfStatement 
    case R.id.action_checkbox1: 
    if (item.isChecked()) item.setChecked(false); 
    else item.setChecked(true); 
     homeLayout.getResources().getColor(R.color.colorPrimary); 
    return true; 

    case R.id.action_checkbox2: 
    if (item.isChecked()) item.setChecked(false); 
    else item.setChecked(true); 
     homeLayout.getResources().getColor(R.color.colorPrimary); 
    return true; 

    case action_checkbox3: 
    if (item.isChecked()) item.setChecked(false); 
    else item.setChecked(true); 
     homeLayout.getResources().getColor(R.color.colorPrimary); 
    return true; 
    default: 
    return super.onOptionsItemSelected(item); 
} 

}

@Override 
public void onStart() { 
    super.onStart(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client.connect(); 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Home Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app deep link URI is correct. 
      Uri.parse("android-app://com.example.jovie.canteen/http/host/path") 
    ); 
    AppIndex.AppIndexApi.start(client, viewAction); 
} 

@Override 
public void onStop() { 
    super.onStop(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Home Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app deep link URI is correct. 
      Uri.parse("android-app://com.example.jovie.canteen/http/host/path") 
    ); 
    AppIndex.AppIndexApi.end(client, viewAction); 
    client.disconnect(); 
} 

}

activity_home.xml(activity_main.xml中)

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.example.jovie.canteen.HomeActivity"> 

<android.support.design.widget.AppBarLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    android:id="@+id/home_Layout"> 
</android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

<include layout="@layout/content_home"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    app:backgroundTint="@color/colorFAB" 
    android:src="@drawable/ic_search_black_24px" /> 

content_home.xxml(content_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.jovie.canteen.HomeActivity" 
tools:showIn="@layout/activity_home"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" /> 

回答

1

你不能投AppBarLayout到CoordinatorLayout,這是兩個不同的班級。如果您想使用CoordinatorLayout,則必須在XML文件中爲其設置一個標識,並在findViewById()中使用此標識。

+0

我會在activity_main.xml中設置像這樣的id:android:id =「@ + id/CoordinatorLayout,是否正確? – brettbrdls

+0

是的,將您的代碼片段添加到XML中的元素中, homeLayout =(CoordinatorLayout)findViewById(R.id.CoordinatorLayout);'。 – patloew

+0

它現在可以工作,但我的appbar沒有陰影了哈哈,但非常感謝幫助的人! – brettbrdls

相關問題