在将FragView添加到Fragment时向类android.support.design.widget.NavigationView输入错误

时间:2016-12-04 10:43:14

标签: java android

我正在尝试向一个片段添加一个NavigationView,首先我有一个被片段替换的活动,但是我收到了这个错误,这是我的代码,job_activity.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context="com.akbari.myapplication.myjobapp.com.akbari.myapplication.myjobapp.activity.JobActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

    </android.support.design.widget.AppBarLayout>

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

</android.support.design.widget.CoordinatorLayout>

jobActivity类:

public class JobActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_job);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Intent intent = getIntent();
    String jobName = intent.getStringExtra("selectedJob");
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(jobName);
    String payDay = intent.getStringExtra("payDay");
    String isAddTime = intent.getStringExtra("addTime");
    Bundle bundle = new Bundle();
    bundle.putString("selectedJob", jobName);
    bundle.putString("payDay", payDay);
    if (isAddTime!=null && isAddTime.equals("true")){
        AddTimeFragment timeFragment=new AddTimeFragment();
        timeFragment.setArguments(bundle);
        ft.replace(R.id.job_holder, timeFragment);
        ft.commit();
    }
    else {
        JobFragment jobFragment = new JobFragment();
        jobFragment.setArguments(bundle);
        ft.replace(R.id.job_holder, jobFragment);
        ft.commit();
    }
}}

jobFragment类:

public class JobFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_job, container, false);
    setHasOptionsMenu(true);
    setMonthTime(view);
    setAddButtonActionListener(view);
    setChartData(view);
    return view;
}

private void setMonthTime(View view) {
    TextView monthTime = (TextView) view.findViewById(R.id.monthTime);
    DonutChart donut = (DonutChart) view.findViewById(R.id.donutChart);
    TimeDao timeDao = new TimeDao();
    String time = timeDao.getThisMonthTime(this.getActivity(),
            getArguments().getString("payDay"),
            getArguments().getString("selectedJob")
    ).toString();
    donut.setData(Float.valueOf(time));
    monthTime.setText(time);
}

private void setAddButtonActionListener(View view) {
    Button addTimeButton = (Button) view.findViewById(R.id.addTimeBtn);
    addTimeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), JobActivity.class);
            intent.putExtra("selectedJob", getArguments().getString("selectedJob"));
            intent.putExtra("payDay", getArguments().getString("payDay"));
            intent.putExtra("addTime", "true");
            startActivity(intent);
        }
    });
}

private void setChartData(View view) {
    BarChart chart = (BarChart) view.findViewById(R.id.chart);
    List<BarEntry> entries = new ArrayList<>();
    TimeDao timeDao = new TimeDao();
    Map<Integer, Integer> chartData = timeDao.getChartData(getContext(), getJobTime());
    for (Map.Entry<Integer, Integer> entry : chartData.entrySet()
            ) {
        entries.add(new BarEntry(entry.getKey(), entry.getValue()));
    }
    BarDataSet set = new BarDataSet(entries, "BarDataSet");
    BarData data = new BarData(set);
    set.setColors(new int[]{R.color.pink, R.color.colorAccent, R.color.yellow},
            getContext());
    data.setBarWidth(0.5f);
    chart.setHorizontalScrollBarEnabled(true);
    chart.setData(data);
    chart.setFitBars(true); // make the x-axis fit exactly all bars
    chart.invalidate();
}

private JobTime getJobTime() {
    JobTime jobTime = new JobTime();
    jobTime.setJobName(getArguments().getString("selectedJob"));
    jobTime.setPayDay(Integer.valueOf(getArguments().getString("payDay")));
    jobTime.setDateTo(DateUtil.getCurrentPersianDate());
    return jobTime;
}

@Override
public void onCreateOptionsMenu(
        Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_open_drawer, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // handle item selection
    switch (item.getItemId()) {
        case R.id.open_drawer:
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}}

jobFragment 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:customviews="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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <Button
            android:id="@+id/addTimeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:text="@string/addTime"
            android:textDirection="anyRtl" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </LinearLayout>

        <com.github.mikephil.charting.charts.BarChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/addTimeBtn"
            android:layout_marginTop="95dp" />

        <TextView
            android:id="@+id/monthTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/chart"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="49dp"
            android:text="@string/monthTime"
            android:textDirection="anyRtl" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/donutChart"
            android:layout_marginTop="25dp"
            android:background="@color/textViewColorPink"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/monthTime"
            android:textDirection="anyRtl"
            android:textSize="12sp" />

        <com.akbari.myapplication.jobapp.view.DonutChart
            android:id="@+id/donutChart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="47dp"
            android:layout_toEndOf="@+id/addTimeBtn"
            customviews:radius="90dp" />

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemTextColor="@color/colorAccent"
        app:menu="@menu/drawer_view" />

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

styles.xml:

    <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <declare-styleable name="DonutChart">
        <attr name="radius" format="dimension"/>
    </declare-styleable>

</resources>

我尝试了很多方法,但仍然遇到此错误:

   E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.akbari.myapplication.jobapp, PID: 10412
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akbari.myapplication.jobapp/com.akbari.myapplication.jobapp.activity.JobActivity}: android.view.InflateException: Binary XML file line #75: Binary XML file line #75: Error inflating class android.support.design.widget.NavigationView
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: android.view.InflateException: Binary XML file line #75: Binary XML file line #75: Error inflating class android.support.design.widget.NavigationView
                   Caused by: android.view.InflateException: Binary XML file line #75: Error inflating class android.support.design.widget.NavigationView
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Constructor.newInstance0(Native Method)
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                      at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                      at com.akbari.myapplication.jobapp.fragment.JobFragment.onCreateView(JobFragment.java:36)
                      at 

    android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                          at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
                          at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
                          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                          at android.app.Activity.performStart(Activity.java:6681)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                       Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/internal/view/menu/MenuPresenter;
                          at dalvik.system.DexFile.defineClassNative(Native Method)
                          at dalvik.system.DexFile.defineClass(DexFile.java:299)
                          at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:292)
                          at dalvik.system.DexPathList.findClass(DexPathList.java:418)
                          at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
                          at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)

有人可以帮助我吗?

编辑:build.gradle:

apply plugin: 'com.android.application'

android {     compileSdkVersion 23     buildToolsVersion&#34; 23.0.1&#34;

defaultConfig {
    applicationId "com.akbari.myapplication.jobapp"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}}
repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
    compile 'com.alirezaafkar:sundatepicker:2.0.4'
    compile 'com.ibm.icu:icu4j:58.1'
    compile 'org.apache.commons:commons-lang3:3.5'
}

0 个答案:

没有答案