Android Error inflating class android.support.constraint.ConstraintLayout

android

When I want to start my application I get error "application has stopped working.Why do I get this error when I start app?

——— beginning of crash 04-12 18:28:12.344 8646-8646/com.example.audiophileradio.audiophileradio
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.audiophileradio.audiophileradio, PID: 8646
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.audiophileradio.audiophileradio/com.example.audiophileradio.audiophileradio.MainActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class android.support.constraint.ConstraintLayout
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #2:
Error inflating class android.support.constraint.ConstraintLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at
android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at
com.example.audiophileradio.audiophileradio.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5933)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at
android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at
com.example.audiophileradio.audiophileradio.MainActivity.onCreate(MainActivity.java:52) 
at android.app.Activity.performCreate(Activity.java:5933) 
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 147456012
byte allocation with 1048480 free bytes and 94MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
at android.content.res.Resources.loadDrawable(Resources.java:2330)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.(View.java:3730)
at android.view.ViewGroup.(ViewGroup.java:491)
at android.view.ViewGroup.(ViewGroup.java:487)
at android.view.ViewGroup.(ViewGroup.java:483)
at
android.support.constraint.ConstraintLayout.(ConstraintLayout.java:419)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at
android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at
com.example.audiophileradio.audiophileradio.MainActivity.onCreate(MainActivity.java:52) 
at android.app.Activity.performCreate(Activity.java:5933) 
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

public class MainActivity extends AppCompatActivity
{

    private ImageButton buttonPlay,buttonStop,buttonPause;
    private String STREAM_URL = "http://178.149.86.162:8000/";
    private MediaPlayer mPlayer;
    Intent playbackServiceIntent;

    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;

    private TextView volume;
    Context context;
    NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonPlay = (ImageButton) findViewById(R.id.buttonPlay);

        mPlayer = new MediaPlayer();

        initControls();

        buttonPlay.setTag(1);
        buttonPlay.setImageResource(R.drawable.stop);
        buttonPlay.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (Integer.parseInt(buttonPlay.getTag().toString()) == 1)
                {
                    buttonPlay.setImageResource(R.drawable.play);
                    view.setTag(0);
                    startService(playbackServiceIntent);
                    Log.e("Play", "onPlay");
                    Toast.makeText(MainActivity.this, "Play", Toast.LENGTH_SHORT).show();
                    showNotification();
                }
                else
                {
                    buttonPlay.setImageResource(R.drawable.stop);
                    view.setTag(1);
                    mPlayer.stop();
                    stopService(playbackServiceIntent);
                    Log.e("Stop", "onPlay");
                    Toast.makeText(MainActivity.this, "Stop", Toast.LENGTH_SHORT).show();
                }

            }
        });

        volume = (TextView) findViewById(R.id.textView);
        volume.setText("Volume :" + volumeSeekbar.getProgress());

        playbackServiceIntent = new Intent(this, BackgroundService.class);
    }

    private void startService()
    {
        startService(new Intent(MainActivity.this,BackgroundService.class));
    }
    private void stopService()
    {
        stopService(new Intent(MainActivity.this,BackgroundService.class));
    }

    private void initControls()
    {
        try
        {
            volumeSeekbar = (SeekBar)findViewById(R.id.seekBar);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));

            volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
            {
                @Override
                public void onStopTrackingTouch(SeekBar arg0)
                {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0)
                {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
                {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);

                    volume.setText("Volume : " + progress + "%");
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed()
    {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Audiophile Radio")
                .setMessage("Are you sure you want to close this activity?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        finish();
                    }

                })
                .setNegativeButton("No", null)
                .show();
    }

    private void showNotification()
    {
        Intent intent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
        Notification builder = new Notification.Builder(MainActivity.this)
                .setTicker("Audiophile Radio")
                .setContentTitle("Audiophile Radio")
                .setContentText("DeeJay Z1an")
                .setSmallIcon(R.drawable.play)
                .addAction(R.drawable.play,"play",pendingIntent)
                .setContentIntent(pendingIntent).getNotification();

        builder.flags=Notification.FLAG_AUTO_CANCEL;
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,builder);
    }

    public static void cancelNotification(Context ctx, int notifyId) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
        nMgr.cancel(notifyId);
    }
}



public class BackgroundService extends Service implements OnCompletionListener
{
    MediaPlayer mediaPlayer;
    private String STREAM_URL = "http://178.149.86.162:8000/";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate()
    {
        mediaPlayer = new MediaPlayer();
        try
        {
            mediaPlayer.setDataSource(STREAM_URL);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        mediaPlayer.setOnCompletionListener(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        if (!mediaPlayer.isPlaying())
        {
            try
            {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(STREAM_URL);
                mediaPlayer.prepareAsync();

                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                {
                    @Override
                    public void onPrepared(MediaPlayer mp)
                    {
                        mediaPlayer.start();
                    }
                });
            } catch (IOException e)
            {
                e.printStackTrace();
            }

        }

        return START_STICKY;
    }

    public void onDestroy() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

    public void onCompletion(MediaPlayer _mediaPlayer) {
        stopSelf();
    }


    @Override
    public boolean onUnbind(Intent intent)
    {
        return super.onUnbind(intent);
    }
}

Best Solution

Check your build.gradle file, make sure the below dependency is added

compile 'com.android.support.constraint:constraint-layout:1.0.2'