应用程序不适用于旧版本

时间:2014-11-05 16:59:15

标签: android view textview layout-inflater android-appcompat

我开发了一款适用于Android 4.4的应用程序,但不适用于2.3.3。抱歉我的英语,我想有人可以帮助我,非常感谢你。

日志:

 11-05 16:53:13.874      513-513/guaguas.costular.com.guaguas E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #3: Error inflating class android.widget.TextView
            at android.view.LayoutInflater.createView(LayoutInflater.java:518)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
            at android.widget.AbsListView.obtainView(AbsListView.java:1430)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
            at android.widget.ListView.onMeasure(ListView.java:1127)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
            at android.view.View.measure(View.java:8313)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:863)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
            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.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
            at android.view.LayoutInflater.createView(LayoutInflater.java:505)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
            at android.widget.AbsListView.obtainView(AbsListView.java:1430)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
            at android.widget.ListView.onMeasure(ListView.java:1127)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
            at android.view.View.measure(View.java:8313)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:863)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
            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.UnsupportedOperationException: Can't convert to dimension: type=0x2
            at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
            at android.view.View.<init>(View.java:1963)
            at android.widget.TextView.<init>(TextView.java:344)
            at android.widget.TextView.<init>(TextView.java:337)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
            at android.view.LayoutInflater.createView(LayoutInflater.java:505)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
            at android.widget.AbsListView.obtainView(AbsListView.java:1430)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
            at android.widget.ListView.onMeasure(ListView.java:1127)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
            at android.view.View.measure(View.java:8313)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:863)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
            at android.view.View.measure(View.java:8313)
            at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
            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)

MainActivity:

    public class MainActivity extends ActionBarActivity {

    private static final int ROUTES = 0;
    private static final int FAVORITES = 1;
    private static final int PRICES = 2;

    // SECOND LIST
    private static final int OPINION = 0;
    private static final int CONTACT = 1;
    private static final int SETTINGS = 2;

    private final static String TAG = "MainActivity";

    //DrawerLayout
    private ListView listView;
    private ListView secondList;
    private String[] secondListTitles;

    private DrawerLayout drawer;
    private ActionBarDrawerToggle toggle;

    private String[] itemTitles;
    private CharSequence title;

    private DrawerListAdapter mAdapter;
    private int mPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Inicializamos la publicidad
        StartAppSDK.init(this, "106270405", "210032482", true);

        setContentView(R.layout.drawer_layout);

        // Creamos el menú
        setupDrawer();

        if(savedInstanceState == null) {
            selectItem(ROUTES);
        }

        if(PrefUtils.isFirstTime(this)) {
            //¿Una guía quizá?
            drawer.openDrawer(Gravity.LEFT);

            // Y quitamos que es la primera
            PrefUtils.setNotFirstTime(this);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Associate searchable configuration with the SearchView
            SearchManager searchManager =
                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

            SearchView searchView =
                    (SearchView) menu.findItem(R.id.search).getActionView();

            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (toggle.onOptionsItemSelected(item)) {
            return true;
        }

        int id = item.getItemId();
        if (id == R.id.search) {

            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                onSearchRequested();
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        toggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        toggle.onConfigurationChanged(newConfig);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            view.setSelected(true);
            selectItem(position);
        }
    }

    private class SecondListItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch(position) {

                // Envía tu opinión
                case OPINION:
                    closeDrawer();


                    break;
                case CONTACT:
                    closeDrawer();

                    Intent opinion = new Intent(getApplicationContext(), ContactActivity.class);
                    startActivity(opinion);
                    break;
                case SETTINGS:
                    closeDrawer();

                    // Abrimos el Activity.
                    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    }

    private void closeDrawer() {
        drawer.closeDrawer(Gravity.START);
    }

    private void setupDrawer() {

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if(drawer == null) {
            return;
        }
        drawer.setStatusBarBackgroundColor(getResources().getColor(R.color.dark_red));

        listView = (ListView) findViewById(R.id.left_drawer);
        secondList = (ListView) findViewById(R.id.second_list);

        secondListTitles = getResources().getStringArray(R.array.second_list_array);
        // El borde para que parezca que tiene volumen Z
        drawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        title = getSupportActionBar().getTitle();
        // Activamos para que se abra el menú desde el icono
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        List<DrawerListItem> items = new ArrayList<DrawerListItem>();
        // Obtenemos el array con los nombres del menú
        itemTitles = getResources().getStringArray(R.array.menu_title);
        // Obtenemos un array especial para pasar de String a Integer ya que contiene los ids de los drawables
        TypedArray icons = getResources().obtainTypedArray(R.array.menu_images);

        // Un bucle para cargar el list
        for(int i = 0; i < itemTitles.length; i++) {
            // Añadimos y si el título es avisos, añadimos el contador
            items.add(new DrawerListItem(itemTitles[i], icons.getResourceId(i, -1)));
        }
        // Borramos de memoria
        icons.recycle();

        // Añadimos el adapter
        mAdapter = new DrawerListAdapter(this, items);
        listView.setAdapter(mAdapter);
        items.clear(); // Para ahorrar memoria :D

        toggle = new ActionBarDrawerToggle(this, drawer,
                R.string.menu,
                R.string.app_name) {

            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(title);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(R.string.app_name);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        drawer.setDrawerListener(toggle);

        listView.setOnItemClickListener(new DrawerItemClickListener());

        //Second list
        secondList.setAdapter(new ArrayAdapter<String>(this, R.layout.second_list_item, secondListTitles));
        secondList.setOnItemClickListener(new SecondListItemClickListener());
    }


    private void selectItem(int position) {

        if(mPosition == position) {
            closeDrawer();
            return;
        }

        switch(position) {

            case ROUTES:
                mPosition = ROUTES;

                Fragment fragment = (Fragment) new RoutesFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
                break;
            case FAVORITES:
                mPosition = FAVORITES;

                Fragment favorites = (Fragment) new FavoritesFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, favorites).commit();
                break;

        }
        // Ponemos el nombre en el ActionBar
        setTitle(itemTitles[position]);

        mAdapter.setmSelectedItem(mPosition);

        //Cerramos el navigation Drawer
        closeDrawer();
    }

    @Override
    public void setTitle(CharSequence str) {
        this.title = str;
        getSupportActionBar().setTitle(title);
    }

}

drawer_layout.xml:

<android.support.v4.widget.DrawerLayout

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical"
        android:background="#FFFFFF">

    <ListView android:id="@+id/left_drawer"
        android:paddingTop="8dp"
        android:layout_height="wrap_content"
        android:layout_width="304dp"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#FFFFFF"/>

    <include layout="@layout/navdrawer_separator"/>

    <ListView
        android:id="@+id/second_list"
        android:layout_width="304dp"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#FFFFFF"/>

</LinearLayout>

谢谢!

修改

片段布局只是FrameLayout的列表视图。

列表布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dp">

    <guaguas.costular.com.guaguas.widget.RobotoTextView android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="48dp"
        android:paddingLeft="10dp"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:textColor="@color/red"
        android:textSize="16sp"
        app:typeface="Roboto-Bold.ttf"/>



    <guaguas.costular.com.guaguas.widget.RobotoTextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:paddingLeft="72dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:minHeight="48dp"
        android:gravity="center_vertical"
        android:layout_centerVertical="true"
        app:typeface="Roboto-Regular.ttf"/>



</RelativeLayout>

0 个答案:

没有答案