Getting android.support.v7.widget.AppCompatTextView error

  • Replies:3
  • OpenNot stickiedUnanswered
  • Forum posts: 2

Sep 17, 2015 12:41:13 AM via Website

Hi All,

Well I am developing slider menu navigation program in andriod. I am getting following RuntimeException while run the program in android studio 1.2.

Error is as :-

/com.mycompany.slidingmenu E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.slidingmenu/com.mycompany.slidingmenu.SlidingMenuActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView
at com.mycompany.slidingmenu.SlidingMenuActivity.onCreate(SlidingMenuActivity.java:55)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)


The code is as :-

file name :- SlidingMenuActivity.java

package com.mycompany.slidingmenu;

import com.mycompany.slidingmenu.model.NavDrawerItem;
import com.mycompany.slidingmenu.adapter.NavDrawerListAdapter;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.DrawerLayout;
import android.widget.ListView;
import android.support.v4.app.ActionBarDrawerToggle;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import java.util.ArrayList;
import android.view.View;

public class SlidingMenuActivity extends ActionBarActivity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList navDrawerItems;
//private ArrayList navDrawerItems;
private NavDrawerListAdapter adapter;

[USER=1021285]@override[/USER]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_menu);

mTitle = mDrawerTitle = getTitle();

// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

navDrawerItems = new ArrayList();
//navDrawerItems = new ArrayList();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));

// Recycle the typed array
navMenuIcons.recycle();

// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);

// enabling action bar app icon and behaving it as toggle button
//getActionBar().setDisplayHomeAsUpEnabled(true);
//getActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
//getActionBar().setTitle(mTitle);
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle(mDrawerTitle);
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {
// on first time display view for first nav item
//displayView(0);
}
}

[USER=1021285]@override[/USER]
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sliding_menu, menu);
return true;
}

[USER=1021285]@override[/USER]
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}

/*
* Called when invalidateOptionsMenu() is triggered
*/
[USER=1021285]@override[/USER]
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

[USER=1021285]@override[/USER]
public void setTitle(CharSequence title) {
mTitle = title;
//getActionBar().setTitle(mTitle);
getSupportActionBar().setTitle(mTitle);
}

/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
[USER=1021285]@override[/USER]
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

[USER=1021285]@override[/USER]
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}

/**
* Diplaying fragment view for selected nav drawer list item
* */
}

file name :- NavDrawerListAdapter.java

package com.mycompany.slidingmenu.adapter;

import com.mycompany.slidingmenu.R;
import com.mycompany.slidingmenu.model.NavDrawerItem;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

[USER=1021285]@override[/USER]
public int getCount() {
    return navDrawerItems.size();
}

[USER=1021285]@override[/USER]
public Object getItem(int position) {
    return navDrawerItems.get(position);
}

[USER=1021285]@override[/USER]
public long getItemId(int position) {
    return position;
}

[USER=1021285]@override[/USER]
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.counter);

    imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if(navDrawerItems.get(position).getCounterVisibility()){
        txtCount.setText(navDrawerItems.get(position).getCount());
    }else{
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

}


file name :- NavDrawerItem.java

The code is as:-

package com.mycompany.slidingmenu.model;

public class NavDrawerItem {

private String title;
private int icon;
private String count = "0";
// boolean to set visiblity of the counter
private boolean isCounterVisible = false;

public NavDrawerItem(){}

public NavDrawerItem(String title, int icon){
    this.title = title;
    this.icon = icon;
}

public NavDrawerItem(String title, int icon, boolean isCounterVisible, String count){
    this.title = title;
    this.icon = icon;
    this.isCounterVisible = isCounterVisible;
    this.count = count;
}

public String getTitle(){
    return this.title;
}

public int getIcon(){
    return this.icon;
}

public String getCount(){
    return this.count;
}

public boolean getCounterVisibility(){
    return this.isCounterVisible;
}

public void setTitle(String title){
    this.title = title;
}

public void setIcon(int icon){
    this.icon = icon;
}

public void setCount(String count){
    this.count = count;
}

public void setCounterVisibility(boolean isCounterVisible){
    this.isCounterVisible = isCounterVisible;
}

}


file name :-
res/layout/activity_sliding_menu.xml

xmlns:android="schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<TextView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>


file name :-

res/layout/drawer_list_item.xml


android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/list_selector">

<ImageView
    android:id="@+id/icon"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:contentDescription="[USER=696546]@String[/USER]/desc_list_item_icon"
    android:src="@drawable/ic_home"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/icon"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@color/list_item_title"
    android:gravity="center_vertical"
    android:paddingRight="40dp"/>

<TextView android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/counter_bg"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:textColor="@color/counter_text_color"/>


file name :-

app/manifests/AndroidManifest.xml


package="com.mycompany.slidingmenu" >

<application
    android:allowBackup="true"
    android:icon="[USER=22138]@Mipmap[/USER]/ic_launcher"
    android:label="[USER=696546]@String[/USER]/app_name"
    android:theme="[USER=19691]@Style[/USER]/AppTheme" >
    <activity
        android:name=".SlidingMenuActivity"
        android:label="[USER=696546]@String[/USER]/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>


file name :-

app/res/menu/manu_sliding_menu.xml

xmlns:app="schemas.android.com/apk/res-auto"
xmlns:tools="schemas.android.com/tools" tools:context=".SlidingMenuActivity">
android:eek:rderInCategory="100" app:showAsAction="never" />


file name :-

app/res/menu/main.xml

<item
    android:id="@+id/action_settings"
    android:eek:rderInCategory="100"
    android:showAsAction="never"
    android:title="[USER=696546]@String[/USER]/action_settings"/>


build.gradle (Module:app) file code is as :-

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.mycompany.slidingmenu"
    minSdkVersion 8
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}

If any other file that is left (by mistake) you require please let me know.

Please answer as soon as possible.

Waiting for your response.

Thanks in advance all you IT GURU/EXPERT.

  • Forum posts: 2

Sep 21, 2015 5:37:31 PM via Website

Hi,

Waiting for reply..............

Answer Please.

Thanks.

  • Forum posts: 208

Sep 24, 2015 1:33:24 PM via Website

import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.ActionBarDrawerToggle;

Action bar is v7 and other are v4???

you can try extends activity other than Actionbar.

  • Forum posts: 2

Sep 24, 2015 2:03:21 PM via Website

First check, which is line 55 in file SlidingMenuActivity.java
As described here:

Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView 
at com.mycompany.slidingmenu.SlidingMenuActivity.onCreate(SlidingMenuActivity.java:55) 

you can put this line in try/catch or put debug breaakpoint into this line and check variables