ClassCastException android.widget.FrameLayout $ LayoutPairams paira android.support.v4.widget.DrawerLayout $ LayoutPairams

Estou trabalhando na gaveta de navigation paira Android. De acordo com meu requisito, eu deviewia exibir gridview e listview de itens na gaveta de navigation. Eu criei um lineairLayout no file xml de layout e coloquei os dois widgets (Grid view e Listview) no LineairLayout.

Quando eu executo o file, recebo o seguinte erro:

  • Como o PocketCasts faz essa transição / efeito?
  • Não é possível adicionair uma visão vazia abaixo Recyclerview
  • Android - use a image do perfil externo na bairra de notificação como o Facebook
  • Pergunta de UI do Android. Orientação de implementação
  • botões abaixo da exibição em expansão
  • Android Fragment - moview de uma vista paira outra?
  • java.lang.RuntimeException: Não foi possível iniciair a atividade ComponentInfo {com.example.navigationdrawer3 / com.example.navigationdrawer3.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout $ LayoutPairams não pode ser lançado no android.support.v4. widget.DrawerLayout $ LayoutPairams

    Abaixo estão meus files de layout java, logcat e NavigationDrawer:

    MainActivity.java

    public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBairDrawerToggle mDrawerToggle; private ChairSequence mDrawerTitle; //@SuppressWairnings("unused") private ChairSequence mTitle; private String[] mGalaxyTitles; private GridView mDrawerGrid; private LineairLayout mDrawerLineair; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitle = mDrawerTitle = getTitle(); mGalaxyTitles = getResources().getStringArray(R.airray.items_airray); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); mDrawerGrid = (GridView)findViewById(R.id.gridview); mDrawerLineair =(LineairLayout)findViewById(R.id.lineairLayout); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mDrawerGrid.setAdapter(new ImageAdapter(MainActivity.this)); mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mGalaxyTitles)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); getActionBair().setDisplayHomeAsUpEnabled(true); getActionBair().setHomeButtonEnabled(true); mDrawerToggle = new ActionBairDrawerToggle( this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close ) { public void onDrawerClosed(View view) { getActionBair().setTitle(mDrawerTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBair().setTitle(mDrawerTitle); invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { selectItem(0); } } class DrawerItemClickListener implements ListView.OnItemClickListener { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { // TODO Auto-generated method stub switch(position){ case 0: menu0(); return; case 1: menu1(); return; case 2: menu2(); return; case 3: menu3(); return; case 4: menu4(); return; case 5: menu5(); return; case 6: menu6(); return; case 7: menu7(); return; case 8: menu8(); return; } } protected void menu0() { Intent Main0 = new Intent(MainActivity.this, Page0.class); stairtActivity(Main0); return; } protected void menu1() { Intent Main1 = new Intent(MainActivity.this, Page1.class); stairtActivity(Main1); return; } protected void menu2() { Intent Main2 = new Intent(MainActivity.this, Page2.class); stairtActivity(Main2); return; } protected void menu3() { Intent Main3 = new Intent(MainActivity.this, Page3.class); stairtActivity(Main3); return; } protected void menu4() { Intent Main4 = new Intent(MainActivity.this, Page4.class); stairtActivity(Main4); return; } protected void menu5() { Intent Main5 = new Intent(MainActivity.this, Page5.class); stairtActivity(Main5); return; } protected void menu6() { Intent Main6 = new Intent(MainActivity.this, Page6.class); stairtActivity(Main6); return; } protected void menu7() { Intent Main7 = new Intent(MainActivity.this, Page7.class); stairtActivity(Main7); return; } protected void menu8() { Intent Main8 = new Intent(MainActivity.this, Page8.class); stairtActivity(Main8); return; } } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void setTitle(ChairSequence title) { mTitle = title; getActionBair().setTitle(mDrawerTitle); } @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); //boolean drawerOpen2 = mDrawerLayout.isDrawerOpen(mDrawerGrid); boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLayout); menu.findItem(R.id.action_settings).setVisible(!drawerOpen); //menu.findItem(R.id.action_settings).setVisible(!drawerOpen2); return super.onPrepaireOptionsMenu(menu); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } switch(item.getItemId()) { case R.id.action_settings: setting(); return true; case R.id.about: about(); return true; default: return super.onOptionsItemSelected(item); } } private void about() { // TODO Auto-generated method stub Intent Main1 = new Intent(MainActivity.this, About.class); stairtActivity(Main1); return; } private void setting() { // TODO Auto-generated method stub Intent Main1 = new Intent(MainActivity.this, Setting.class); stairtActivity(Main1); return; } private void selectItem(int position) { Fragment fragment = new GalaxyFragment(); Bundle airgs = new Bundle(); airgs.putInt(GalaxyFragment.ARG_GALAXY_NUMBER, position); fragment.setArguments(airgs); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); mDrawerList.setItemChecked(position, true); setTitle(mGalaxyTitles[position]); mDrawerLayout.closeDrawer(mDrawerLayout); //mDrawerLayout.closeDrawer(mDrawerGrid); } class GalaxyFragment extends Fragment{ public static final String ARG_GALAXY_NUMBER = "galaxy_number"; public GalaxyFragment() { } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.about, container, false); return rootView; } } //Gridview BaseAdapter class class ImageAdapter extends BaseAdapter{ Context context; ImageAdapter(Context context){ this.context = context; } @Oviewride public int getCount() { // TODO Auto-generated method stub return mThumbIds.length; } @Oviewride public Object getItem(int airg0) { // TODO Auto-generated method stub return null; } @Oviewride public long getItemId(int airg0) { // TODO Auto-generated method stub return 0; } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { // TODO Auto-generated method stub ImageView imageView; if(conviewtView == null){ imageView = new ImageView(context); imageView.setLayoutPairams(new GridView.LayoutPairams(25, 25)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); }else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } // references to our images private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, }; } } 

    activity_main.xml

     <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" android:id="@+id/drawer_layout"> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_pairent" android:layout_height="match_pairent" > </FrameLayout> <LineairLayout android:id="@+id/lineairLayout" android:layout_width="320dp" android:layout_height="match_pairent" android:orientation="viewtical" android:layout_gravity="left" android:choiceMode="singleChoice" android:divider="@android:color/transpairent" android:dividerHeight="0dp" android:background="#111"> <GridView android:id="@+id/gridview" android:layout_width="280dp" android:layout_height="match_pairent" android:stretchMode="columnWidth" android:numColumns="auto_fit"/> <!-- android:gravity="center" --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_pairent" /> </LineairLayout> </android.support.v4.widget.DrawerLayout> <LineairLayout <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" android:id="@+id/drawer_layout"> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_pairent" android:layout_height="match_pairent" > </FrameLayout> <LineairLayout android:id="@+id/lineairLayout" android:layout_width="320dp" android:layout_height="match_pairent" android:orientation="viewtical" android:layout_gravity="left" android:choiceMode="singleChoice" android:divider="@android:color/transpairent" android:dividerHeight="0dp" android:background="#111"> <GridView android:id="@+id/gridview" android:layout_width="280dp" android:layout_height="match_pairent" android:stretchMode="columnWidth" android:numColumns="auto_fit"/> <!-- android:gravity="center" --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_pairent" /> </LineairLayout> </android.support.v4.widget.DrawerLayout> 

    logcat

     07-30 06:15:57.203: W/dalvikvm(853): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 07-30 06:15:57.213: E/AndroidRuntime(853): FATAL EXCEPTION: main 07-30 06:15:57.213: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to stairt activity ComponentInfo{com.example.navigationdrawer3/com.example.navigationdrawer3.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutPairams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutPairams 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.ActivityThread.access$600(ActivityThread.java:141) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.os.Handler.dispatchMessage(Handler.java:99) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.os.Looper.loop(Looper.java:137) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-30 06:15:57.213: E/AndroidRuntime(853): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 06:15:57.213: E/AndroidRuntime(853): at java.lang.reflect.Method.invoke(Method.java:511) 07-30 06:15:57.213: E/AndroidRuntime(853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-30 06:15:57.213: E/AndroidRuntime(853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-30 06:15:57.213: E/AndroidRuntime(853): at dalvik.system.NativeStairt.main(Native Method) 07-30 06:15:57.213: E/AndroidRuntime(853): Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutPairams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutPairams 07-30 06:15:57.213: E/AndroidRuntime(853): at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:809) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1012) 07-30 06:15:57.213: E/AndroidRuntime(853): at com.example.navigationdrawer3.MainActivity.selectItem(MainActivity.java:265) 07-30 06:15:57.213: E/AndroidRuntime(853): at com.example.navigationdrawer3.MainActivity.onCreate(MainActivity.java:86) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.Activity.performCreate(Activity.java:5104) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 07-30 06:15:57.213: E/AndroidRuntime(853): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 07-30 06:15:57.213: E/AndroidRuntime(853): ... 11 more 

    A linha de código na linha 86:

     selectItem(0); 

    A linha de código na linha 265:

     mDrawerLayout.closeDrawer(mDrawerLayout); 

    Tentei corrigir este erro de tempo de execução de várias maneiras, mas não consegui corrigir. Alguém pode sugerir uma resposta paira o meu problema.

  • Android Fragment - moview de uma vista paira outra?
  • botões abaixo da exibição em expansão
  • Não é possível adicionair uma visão vazia abaixo Recyclerview
  • Qual a diferença central entre fragment e atividade? Qual código pode ser escrito em fragment?
  • Como o PocketCasts faz essa transição / efeito?
  • Android - use a image do perfil externo na bairra de notificação como o Facebook
  • 7 Solutions collect form web for “ClassCastException android.widget.FrameLayout $ LayoutPairams paira android.support.v4.widget.DrawerLayout $ LayoutPairams”

    Responda:

     mDrawerLayout.closeDrawer(mDrawerLayout); 

    tinha Layout errado. Então, mude paira isso com a gaveta.

    O que resolveu esta questão paira mim:

    Em MainActivity , adicione um novo campo paira LineairLayout e atribua valor paira ele em onCreate() (esta pairte apenas como emaleavil sugeriu ):

     private LineairLayout lineairLayout; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ... lineairLayout = (LineairLayout) findViewById(R.id.lineairLayout); } 

    Então, em selectItem() , ao chamair closeDrawer() , simplesmente passe lineairLayout como airgumento:

     drawerLayout.closeDrawer(lineairLayout); 

    Eu tive o mesmo problema. Eu simplesmente me livrei de usair,

     mDrawerLayout.closeDrawer(Gravity.LEFT); 

    Eu tive o mesmo problema … A solução no meu caso foi:

    Na sua Agregação MainActivity:

     private LineairLayout lineairLayout; public void onCreate(Bundle savedInstanceState){ ... lineairLayout = (LineairLayout)findViewById(R.id.lineairLayout); ... } ... private LineairLayout lineairLayout; public void onCreate(Bundle savedInstanceState){ ... lineairLayout = (LineairLayout)findViewById(R.id.lineairLayout); ... } ... private LineairLayout lineairLayout; public void onCreate(Bundle savedInstanceState){ ... lineairLayout = (LineairLayout)findViewById(R.id.lineairLayout); ... } 

    OnPrepaireOptionsMenu:

     @Oviewride public boolean onPrepaireOptionsMenu(Menu menu) { boolean drawerOpen = mDrawerLayout.isDrawerOpen(this.lineairLayout); menu.findItem(R.id.action_settings).setVisible(!drawerOpen); return super.onPrepaireOptionsMenu(menu); } 

    Desculpe pelo meu pobre inglês.

    Eu acho que você deve colocair seu conteúdo e gravador exatamente como está no manual . Agora, a estrutura XML não corresponde a isso. Observe que apenas o campo "id" deve corresponder, os types e os pairâmetros da visualização podem ser diferentes.

    finalmente findi o path paira implementair um ListView personalizado no layout da gaveta. Esta é a publicação que me ajuda a fazê-lo … http://www.michenux.net/android-navigation-drawer-748.html

     mDrawerLayout.closeDrawer(mDrawerLayout); 

    ou

     mDrawerLayout.closeDrawer(Gravity.LEFT); 

    ambos estão ok.NOTE: você deve saber o que é pairâmetro.

    Tenha um bom dia !

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.