ClassCastException异常:java.lang.ClassCastException:android.widget.LinearLayout $的LayoutParams不能转换为android.support.v4.widget.DrawerLayout [英] ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout
问题描述
我已经写了这是可以正常使用的导航抽屉里的计划,但最近我试图把一个的ImageView
的资料图片和的TextView
,之后它给了我一个 ClassCastException异常
。
I have written a program for a navigation drawer which was working perfectly, but recently i have tried to put an ImageView
for a profile picture and a TextView
, after that it gives me a ClassCastException
.
main_activity.xml:当我删除的LinearLayout
里面的的ImageView
和的TextView
是,其完美的工作。
main_activity.xml : When I remove the LinearLayout
inside that the ImageView
and the TextView
are, its working perfectly.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.example.shoppingcart.MainActivity" >
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/homeLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/home_logo" />
</FrameLayout>
<LinearLayout
android:id="@+id/linear1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#262626"
android:orientation="vertical" >
<ImageView
android:id="@+id/profilePic"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="15dp"
android:src="@drawable/shopping_cart" />
<TextView
android:id="@+id/textMailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/profilePic"
android:layout_marginLeft="60dp"
android:layout_marginTop="5dp"
android:text="narahari.arjun@gmail.com"
android:textColor="#ffffff" />
<LinearLayout
android:id="@+id/linear5"
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_below="@+id/textMailAddress"
android:layout_marginTop="7dp"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#7A7A7A" />
<ListView
android:id="@+id/drawerlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linear1"
android:layout_gravity="start"
android:background="#262626"
android:choiceMode="singleChoice"
android:divider="#7A7A7A"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java:我的code导航抽屉的ImageView和TextView的
MainActivity.java : My code for navigation drawer with ImageView and TextView.
public class MainActivity extends ActionBarActivity {
RelativeLayout relativeLayout;
private LinearLayout linearLayout;
String title = "Home";
ListView drawerList;
DrawerLayout drawer;
ActionBarDrawerToggle drawerListener;
private int openDrawerContentDescRes;
private int closeDrawerContentDescRes;
String[] Menus = { "Books", "Laptops", "Mobiles", "Fashion", "Sports",
"Logout" };
int[] images = { R.drawable.books_logo, R.drawable.laptops_logo,
R.drawable.mobilelogo, R.drawable.fashion_logo,
R.drawable.sports_logo, R.drawable.logout };
public static final String PREF_NAME = "Login";
String passedData, passedData1;
String data, data1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerList = (ListView) findViewById(R.id.drawerlist);
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
passedData = getIntent().getStringExtra("emailID");
passedData1 = getIntent().getStringExtra("pass");
linearLayout = (LinearLayout) findViewById(R.id.linear1);
SharedPreferences settings = getSharedPreferences(PREF_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
data = settings.getString("emailId", passedData);
data1 = settings.getString("pass", passedData1);
System.out.println("EmailID:" + data);
System.out.println("Password:" + data1);
System.out.println("EmailID:" + passedData);
System.out.println("Password:" + passedData1);
drawerListener = new ActionBarDrawerToggle(this, drawer,
R.drawable.navigation_menu, openDrawerContentDescRes,
closeDrawerContentDescRes) {
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
}
};
drawer.setDrawerListener(drawerListener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setTitle(" " + title);
MyAdapter adapter = new MyAdapter(getBaseContext(), Menus, images);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
displayView(position);
drawer.closeDrawer(drawerList);
}
private void selectItem(int position) {
// TODO Auto-generated method stub
drawerList.setItemChecked(position, true);
setTitle(Menus[position]);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (drawerListener.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new PhotosFragment();
break;
case 2:
Intent intent = new Intent(MainActivity.this, MobileActivity.class);
intent.putExtra("mobiles", "Mobiles");
startActivity(intent);
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
logout();
break;
default:
break;
}
if (fragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayout,
fragment).commit();
// update selected item and title, then close the drawer
drawerList.setItemChecked(position, true);
drawerList.setSelection(position);
setTitle(Menus[position]);
drawer.closeDrawer(drawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
logcat的:
Logcat :
05-13 22:51:51.914: E/AndroidRuntime(6666): FATAL EXCEPTION: main
05-13 22:51:51.914: E/AndroidRuntime(6666): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1126)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1331)
05-13 22:51:51.914: E/AndroidRuntime(6666): at com.example.shoppingcart.MainActivity$2.onItemClick(MainActivity.java:110)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.widget.AbsListView.performItemClick(AbsListView.java:1223)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.widget.ListView.performItemClick(ListView.java:4506)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2967)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.widget.AbsListView$1.run(AbsListView.java:3653)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.os.Handler.handleCallback(Handler.java:725)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.os.Looper.loop(Looper.java:158)
05-13 22:51:51.914: E/AndroidRuntime(6666): at android.app.ActivityThread.main(ActivityThread.java:5751)
05-13 22:51:51.914: E/AndroidRuntime(6666): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 22:51:51.914: E/AndroidRuntime(6666): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 22:51:51.914: E/AndroidRuntime(6666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
05-13 22:51:51.914: E/AndroidRuntime(6666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
05-13 22:51:51.914: E/AndroidRuntime(6666): at dalvik.system.NativeStart.main(Native Method)
我应该改变我的XML $ C $的C或Java code?
Should I change my XML code or java code?
感谢您!
推荐答案
您正试图关闭列表,它不是一个抽屉。尝试使用 drawer.closeDrawers();的
而不是 drawer.closeDrawer(drawerList)
。
You are trying to close the list which isn't a "drawer". Try using drawer.closeDrawers();
instead of drawer.closeDrawer(drawerList)
.
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
displayView(position);
drawer.closeDrawers();
}
private void selectItem(int position) {
// TODO Auto-generated method stub
drawerList.setItemChecked(position, true);
setTitle(Menus[position]);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
});
这篇关于ClassCastException异常:java.lang.ClassCastException:android.widget.LinearLayout $的LayoutParams不能转换为android.support.v4.widget.DrawerLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!