当前位置: 首页 > 知识库问答 >
问题:

Android inflating类Android.Support.Constraint.ConstraintLayout时出错

姜飞飙
2023-03-14

当我想启动我的应用程序时,我得到的错误“应用程序已经停止工作。为什么我得到这个错误,当我启动应用程序?

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);
    }
}

共有1个答案

仲孙鸿飞
2023-03-14

检查您的build.gradle文件,确保添加了以下依赖项

compile 'com.android.support.constraint:constraint-layout:1.0.2'
 类似资料: