I tried the app on Android Lollipop and it works fine but while testing on kitkat it shows error:
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[]
Here is my category class . Thanks in advance for Help
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;
}
}
And this is my Cat class
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;
}
}
And I am calling Category from MainActivity here is that code:
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);
}
});
}
}
Maybe I´m going wrong, but I need to show the piece of code, so it´s not good to make a comment. There were some changes with serializable and arrays since lollipop, so I guess You cannot cast to array with lower APIs. What You can try is to copy the array like this:
Object[] arrayObject = (Object[]) intent.getSerializableExtra("categories");
Cat[] catArray = Arrays.copyOf(arrayObject , arrayObject.length, Cat[].class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.