渲染期间引发的异常:android.support.design.widget.CoordinatorLayout无法强制转换为android.view.ViewGroup

时间:2016-05-01 19:44:28

标签: android xml android-studio

虽然之前我正在预览,但我没有对文件进行任何更改。事实上,我的代码中没有太多变化,它只是应用程序中的一个OK按钮。 只是告诉我,我在activity_main.xml文件中从我的设计中删除了工具栏。但是之后撤消更改,因为我可以在MainActivity.java文件中看到它。 我是android studio的新手,任何帮助都会很有用。 这是我的java文件。

package com.learning.pehlaapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class SampleActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    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();
        }
    });
}

@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_sample, 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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:0)

Exception raised during Rendering:    
android.support.design.widget.CoordinatorLayout cannot be cast to 
android.view.ViewGroup 

As error said coordinatorLayout can not br cast to ViewGroup Please check your   
XML view as you uses FloatingActionButton which are define under  
CoordinatorLayout and that layout treated as ViewGroup in Activty  thus this 
exception arised