gpt4 book ai didi

java - ClassCastException : java. lang.ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 android.support.v4.widget.DrawerLayout

转载 作者:数据小太阳 更新时间:2023-10-29 02:34:45 30 4
gpt4 key购买 nike

我已经为抽屉导航编写了一个运行良好的程序,但最近我尝试为个人资料图片和 TextView 放置一个 ImageView,之后它给了我一个 ClassCastException

main_activity.xml :当我删除 ImageViewTextView 所在的 LinearLayout 时,它工作正常。

<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:我的带有 ImageView 和 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");
}
}

日志:

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 代码还是 java 代码?

谢谢!

最佳答案

这里有一个错误 drawer.closeDrawer(drawerList); 我认为你应该这样做:drawer.closeDrawer(drawer)。您使用了错误的布局,只需更改为 DrawerLayout。

关于java - ClassCastException : java. lang.ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 android.support.v4.widget.DrawerLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30221418/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com