2016-01-24 30 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

相关问题