java.lang.UnsupportedOperationException:无法在API级别19上转换为color:type = 0x2

时间:2016-08-18 07:12:08

标签: android android-studio

当我按按钮时,应用程序崩溃,而崩溃性显示显示在

>Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.shmoti_v4/com.example.android.shmoti_v4.MainActivities.Movies_Home}: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
           at android.app.ActivityThread.access$900(ActivityThread.java:175)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:146)
           at android.app.ActivityThread.main(ActivityThread.java:5602)
           at java.lang.reflect.Method.invokeNative(Method.java)
           at java.lang.reflect.Method.invoke(Method.java:515)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
           at dalvik.system.NativeStart.main(NativeStart.java)
    Caused by java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
           at android.content.res.TypedArray.getColor(TypedArray.java:327)
           at android.widget.TextView.<init>(TextView.java:1003)
           at android.widget.TextView.<init>(TextView.java:908)
           at android.widget.TextView.<init>(TextView.java:904)
           at android.support.v7.widget.Toolbar.setTitle(Toolbar.java:750)
           at com.example.android.shmoti_v4.MainActivities.Movies_Home.onCreate(Movies_Home.java:74)
           at android.app.Activity.performCreate(Activity.java:5451)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
           at android.app.ActivityThread.access$900(ActivityThread.java:175)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:146)
           at android.app.ActivityThread.main(ActivityThread.java:5602)
           at java.lang.reflect.Method.invokeNative(Method.java)
           at java.lang.reflect.Method.invoke(Methenter code hereod.java:515)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
           at dalvik.system.NativeStart.main(NativeStart.java)`enter code here`


Styles.xml file


<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- 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.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="CustomTheme" parent="MaterialDrawerTheme">
    <!-- ...and here we setting appcompat’s color theming attrs -->
    <item name="colorPrimary">@color/material_drawer_primary</item>
    <item name="colorPrimaryDark">@color/shmotiColor</item>
    <item name="colorAccent">@color/material_drawer_accent</item>

    <!-- MaterialDrawer specific values -->
    <item name="material_drawer_background">@color/material_drawer_dark_background</item>
    <item name="material_drawer_primary_text">@color/material_drawer_dark_primary_text</item>
    <item name="material_drawer_primary_icon">@color/material_drawer_dark_primary_icon</item>
    <item name="material_drawer_secondary_text">@color/material_drawer_dark_secondary_text</item>
    <item name="material_drawer_hint_text">@color/material_drawer_dark_hint_text</item>
    <item name="material_drawer_divider">@color/material_drawer_dark_divider</item>
    <item name="material_drawer_selected">@color/material_drawer_dark_selected</item>
    <item name="material_drawer_selected_text">@color/shmotiColor</item>
    <item name="material_drawer_header_selection_text">@color/material_drawer_dark_header_selection_text</item>
    </style>

    <!--Alert Dialog Box Theme-->
    <style name="AlertDialogCustom" parent="Theme.AppCompat.Dialog.Alert">
        <item name="android:background">@color/colorMaterialDark</item>
        <item name="android:textColor">@color/shmotiColor</item>
        <item name="colorAccent">@color/shmotiColor</item>
    </style>



</resources>


//Movies_Home.java file is as below//

请找到以下代码,请让我知道为什么应用程序在API级别19崩溃

// Movies_Home.java文件如下// 请找到下面的代码,请让我知道为什么应用程序在API级别19崩溃

package com.example.android.shmoti_v4.MainActivities;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

//import com.example.android.shmoti_v3.Utils.NodalClass;
import com.example.android.shmoti_v4.Utils.SlidingTabLayout;
import com.example.android.shmoti_v4.Utils.ViewPagerAdapter;
import com.example.android.shmoti_v4.R;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;

/**

 */
public class Movies_Home extends AppCompatActivity {

    // Declaring Your View and Variables

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[] = {"Current Running", "Upcoming"};
    int Numboftabs = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_movies);

//        //ImageLoaderOptions
//        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
//                .cacheInMemory(true)
//                .cacheOnDisk(true)
//                .build();
//        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
//                .defaultDisplayImageOptions(defaultOptions)
//                .build();
//        ImageLoader.getInstance().init(config); // Do it on Application start


        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.toolbar_main);
        toolbar.setTitle("Movies");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.movies_pager);
        pager.setAdapter(adapter);


        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.movie_tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        //Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.shmotiColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
        }
        return true;
    }


}

//    @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;
//    }
//

1 个答案:

答案 0 :(得分:1)

getResources()的getColor(R.color.shmotiColor)。方法已弃用。

看起来最好的方法是使用:

ContextCompat.getColor(context,R.color.color_name)

这将适当地选择Marshmallow两参数方法或pre-Marshmallow方法。