Android Studio(错误膨胀类片段)

时间:2015-10-15 16:23:40

标签: android android-fragments android-studio genymotion

我遵循了一些为导航抽屉实现android studio Material Design的教程,然后我安装了genymotion来测试我的应用程序以获取其他API。
我在API 16上测试了我的应用。
然后我收到了这个错误。

10-15 16:12:15.295 1411-1411/? E/AndroidRuntime: FATAL EXCEPTION: main
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.directioner.newjd/com.example.directioner.newjd.MainActivity}: android.view.InflateException: Binary XML file line #35: Error inflating class fragment
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class fragment
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.example.directioner.newjd.MainActivity.onCreate(MainActivity.java:20)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5008)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:  Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5606)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1809)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1721)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:58)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:748)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.example.directioner.newjd.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:58)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.example.directioner.newjd.MainActivity.onCreate(MainActivity.java:20) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5008) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)     

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            tools:context="com.example.directioner.newjd.MainActivity">

            <include
                android:id="@+id/app_bar"
                layout="@layout/app_bar" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/app_bar"
                 android:textSize="35dp"
                android:text="MAP HERE" />

        </RelativeLayout>
    </FrameLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.directioner.newjd.NavigationDrawerFragment"
    android:layout_width="@dimen/nav_dr"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="false"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer">

</fragment>


</android.support.v4.widget.DrawerLayout>

我的主要活动。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;


public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar =(Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowHomeEnabled(true);



        NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout),toolbar);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        return super.onOptionsItemSelected(item);
    }

}

我的NavigationDrawerFragment。

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NavigationDrawerFragment extends Fragment {


    private RecyclerView recyclerView;
    public static final String PREF_FILE_NAME = "testpref";
    public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private boolean mUserLearnedDrawer;
    private boolean mFromSavedInstanceState;
    private View containerView;
    private VivzAdapter adapter;

    public NavigationDrawerFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
        if (savedInstanceState != null) {
            mFromSavedInstanceState = true;
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
        adapter=new VivzAdapter(getActivity(),getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                //Toast.makeText(getActivity(),"On Click "+position,Toast.LENGTH_SHORT).show();
                if(position==1){
                    startActivity(new Intent(getActivity(),SubActivity.class));

                }
            }

            @Override
            public void onLongClick(View view, int position) {
                //Toast.makeText(getActivity(),"On Long Click "+position,Toast.LENGTH_SHORT).show();
            }
        }));
        return layout;
    }


    public static List<Information> getData() {
        List<Information> data = new ArrayList<>();
        int[] icons = {R.drawable.ic_home,R.drawable.ic_search_black,R.drawable.ic_favorite, R.drawable.ic_route, R.drawable.ic_place, R.drawable.ic_settings};
        String[] titles = {"Home","Search Location","Favorites", "Route", "Recent Location","Settings"};
        for (int i = 0; i < titles.length && i < icons.length; i++) {
            Information current = new Information();
            current.iconId=icons[i];
            current.title=titles[i];
            data.add(current);
        }
        return data;
    }

    public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
        containerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!mUserLearnedDrawer) {
                    mUserLearnedDrawer = true;
                    savedToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer + "");
                }
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }

        };
        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(containerView);
        }

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

    }

    public static void savedToPreferences(Context context, String preferenceName, String preferenceValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(preferenceName, preferenceValue);
        editor.apply();

    }

    public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
        return sharedPreferences.getString(preferenceName, defaultValue);
    }


    class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{

        private GestureDetector gestureDetector;
        private ClickListener clickListener;
        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener){
                Log.d("CHARLES: ","Constructor");
            this.clickListener=clickListener;
            gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    Log.d("CHARLES: ","onSingleTap"+e);
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    //super.onLongPress(e);
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if(child!=null && clickListener!=null){
                        clickListener.onLongClick(child,recyclerView.getChildPosition(child));
                    }
                    Log.d("CHARLES: ","onLongPress"+e);

                }
            });

        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(e)){
                    clickListener.onClick(child,rv.getChildPosition(child));
            }
            Log.d("CHARLES: ","OnInterceptTouchEvent"+gestureDetector.onTouchEvent(e)+" "+e);
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            Log.d("CHARLES: ","OnTouchEvent");
        }
    }

    public static interface ClickListener{
        public void onClick(View view,int position);
        public void onLongClick(View view,int position);
    }
}

My Fragment_navigation_drawer.xml

<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="@color/white"
    tools:context="com.example.directioner.newjd.NavigationDrawerFragment">

    <LinearLayout
        android:id="@+id/containerDrawerImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00BCD4"
        android:orientation="vertical">



        <ImageView
            android:layout_width="280dp"
            android:layout_height="100dp"
            android:layout_marginTop="@dimen/drawer_margin_top"
            android:background="#00BCD4"
            />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:textColor="#FFF"
            android:textSize="15dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:text="Charles Ivan Galvez"/>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@+id/containerDrawerImage"
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

谢谢!

1 个答案:

答案 0 :(得分:1)

在例外中,下面的一行表示存在转换问题:

10-15 16:12:15.295 1411-1411/? E/AndroidRuntime:  Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1

您的片段布局宽度为android:layout_width="@dimen/nav_dr",因此我建议您检查nav_dr中定义的值。