当我想启动我的应用程序时,我得到的错误“应用程序已经停止工作。为什么我得到这个错误,当我启动应用程序?
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);
}
}
检查您的build.gradle文件,确保添加了以下依赖项
compile 'com.android.support.constraint:constraint-layout:1.0.2'
我不知道,为什么我会有这个问题。我添加了行来构建gradle,但它仍然不起作用。出什么事了?
我试图让我的布局工作,将实现支持设计库功能。然而,当我尝试运行该项目,我得到以下异常时,我的布局是加载。 二进制XML文件行#2:inflating类android.support.design.widget.错误 这是我的布局: 我尝试构建了一个简单的测试项目,该项目实现了CollapsingToolbarLayout,它没有任何问题,但一旦我尝试将其放入我的项目中,它就停止工作了。 我检查了n
错误跟踪: 致命异常:主进程:com.example.user.itekit,pid:23028 android.view.inflateException:二进制XML文件行#87:在android.view.layoutinflater.createViewFromTag(layoutinflater.java:764)在android.view.layoutinflater.rinflate
我的应用程序崩溃了 膨胀类Android.Support.Design.Widget.FloatingActionButton时出错 这是我的XML代码 这是我的卷轴 有人能说出我的错吗?
在Lollipop(API22)中,每次在我的应用程序中显示webview时,应用程序都会崩溃。我的android开发人员控制台中有多个与此事件相关的崩溃。 不用多说,它在Android4、6、7上都能用。 搜索这个问题似乎与lollipop如何处理WebView有关。我开始了一个新鲜的AVD与Lollipop的基础上的一个设备,我发现在GDC的崩溃记者,我可以重现的问题。 全堆栈跟踪: