2016-05-01 46 views
1

儘管我之前獲得了預覽,但我還沒有對文件進行任何更改。事實上,我的代碼中沒有太多需要改變的地方,它只是應用程序中的一個OK按鈕。 只是爲了說明,我從我的設計中刪除了activity_main.xml文件中的工具欄。但之後撤消了更改,因爲我可以在MainActivity.java文件中看到它。 我是android studio的新手,任何幫助都會有用。 這是我的java文件。在渲染過程中引發的異常:android.support.design.widget.CoordinatorLayout無法轉換爲android.view.ViewGroup

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); 
} 
} 

回答

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 
+0

謝謝...有在content_main.xml文件的問題。 將app:layout_behavior&tools:context更改爲默認值。 清理該項目,它工作正常。 –

+0

歡迎。請讓堆棧溢出知道它的正確答案 –

相關問題