gpt4 book ai didi

java - android kitkat 中的类转换异常

转载 作者:行者123 更新时间:2023-11-29 20:11:49 26 4
gpt4 key购买 nike

我在 Android Lollipop 上试用了该应用程序,它工作正常,但在 kitkat 上测试时显示错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yourpackage/com.yourpackage.category}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.itmightys.www.ulhasnagar.Cat[]

这是我的类别类。在此先感谢您的帮助

public class category extends AppCompatActivity {


GridView gv;
Context context;
private CategoryAdapter adapter;
MyDBHandler dbHandler;
private ActionBarDrawerToggle listener;
private DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Navigation navigation = new Navigation(this);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
Intent intent=getIntent();
Cat[] categories=(Cat[])intent.getSerializableExtra("categories");

gv=(GridView) findViewById(R.id.gridView1);
adapter=new CategoryAdapter(this);
adapter.initializedata(categories);
gv.setAdapter(adapter);


//nav drawer animate
listener = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrw,R.string.closedrw){
@Override
public void onDrawerOpened(View drawerView) {
//Toast.makeText(context,"Opened",Toast.LENGTH_SHORT).show();
}

@Override
public void onDrawerClosed(View drawerView) {
//Toast.makeText(MainActivity.this,"Closed",Toast.LENGTH_SHORT).show();
}
};
drawerLayout.setDrawerListener(listener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
listener.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (listener.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}

@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_category, menu);
return true;
}



}

这是我的猫类

public class Cat implements Serializable{
private int category_id;
private String category_name;
private int hassub;
private String category_img;

//empty constructor
public Cat(){

}
//constructor

public Cat(int category_id, String category_name, int hassub, String category_img) {
this.category_id = category_id;
this.category_name = category_name;
this.hassub = hassub;
this.category_img = category_img;
}

//setter


public void setCategory_id(int category_id) {
this.category_id = category_id;
}

public void setCategory_img(String category_img) {
this.category_img = category_img;
}

public void setCategory_name(String category_name) {
this.category_name = category_name;
}

public void setHassub(int hassub) {
this.hassub = hassub;
}


//getters

public int getCategory_id() {
return category_id;
}

public String getCategory_img() {
return category_img;
}

public String getCategory_name() {
return category_name;
}

public int getHassub() {
return hassub;
}
}

我正在从 MainActivity 调用 Category这是代码:

public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView directory=(ImageView) findViewById(R.id.imageView4);
directory.setClickable(true);
directory.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

final Cat cat[];
cat = retrievecat();
Intent intent = new Intent(MainActivity.this, category.class);
intent.putExtra("categories", cat);
startActivity(intent);
}
});
}
}

最佳答案

也许我走错了,但我需要展示这段代码,所以不好发表评论。自 Lollipop 以来,可序列化和数组发生了一些变化,所以我猜你不能转换为具有较低 API 的数组。您可以尝试像这样复制数组:

Object[] arrayObject = (Object[]) intent.getSerializableExtra("categories");  

Cat[] catArray = Arrays.copyOf(arrayObject , arrayObject.length, Cat[].class);

关于java - android kitkat 中的类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34674374/

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