gpt4 book ai didi

java - 不幸的是应用程序已在 Eclipse 中停止

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:50 25 4
gpt4 key购买 nike

您好,我是 Android 应用程序开发新手。我在 AVD 中收到类似消息 - 不幸的是已停止,请帮助我解决此错误...

这是我的 MainActivity.java

package com.p.play;
import java.io.IOException;
import java.math.BigDecimal;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private static final int UPDATE_FREQUENCY = 500;
private static final int STEP_VALUE = 4000;

private MediaCursorAdapter mediaAdapter = null;
private TextView selelctedFile = null;
private SeekBar seekbar = null;
private MediaPlayer player = null;
private ImageButton playButton = null;
private ImageButton prevButton = null;
private ImageButton nextButton = null;

private boolean isStarted = true;
private String currentFile = "";
private boolean isMoveingSeekBar = false;

private final Handler handler = new Handler();

private final Runnable updatePositionRunnable = new Runnable() {
public void run() {
updatePosition();
}
};

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

selelctedFile = (TextView)findViewById(R.id.selectedfile);
seekbar = (SeekBar)findViewById(R.id.seekbar);
playButton = (ImageButton)findViewById(R.id.play);
prevButton = (ImageButton)findViewById(R.id.prev);
nextButton = (ImageButton)findViewById(R.id.next);

player = new MediaPlayer();

player.setOnCompletionListener(onCompletion);
player.setOnErrorListener(onError);
seekbar.setOnSeekBarChangeListener(seekBarChanged);

Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

if(null != cursor)
{
cursor.moveToFirst();

mediaAdapter = new MediaCursorAdapter(this, R.layout.activity_main, cursor);

setListAdapter(mediaAdapter);

playButton.setOnClickListener(onButtonClick);
nextButton.setOnClickListener(onButtonClick);
prevButton.setOnClickListener(onButtonClick);
}
}

@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);

currentFile = (String) view.getTag();

startPlay(currentFile);
}

@Override
protected void onDestroy() {
super.onDestroy();

handler.removeCallbacks(updatePositionRunnable);
player.stop();
player.reset();
player.release();

player = null;
}

private void startPlay(String file) {
Log.i("Selected: ", file);

selelctedFile.setText(file);
seekbar.setProgress(0);

player.stop();
player.reset();

try {
player.setDataSource(file);
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

seekbar.setMax(player.getDuration());
playButton.setImageResource(android.R.drawable.ic_media_pause);

updatePosition();

isStarted = true;
}

private void stopPlay() {
player.stop();
player.reset();
playButton.setImageResource(android.R.drawable.ic_media_play);
handler.removeCallbacks(updatePositionRunnable);
seekbar.setProgress(0);

isStarted = false;
}

private void updatePosition(){
handler.removeCallbacks(updatePositionRunnable);

seekbar.setProgress(player.getCurrentPosition());

handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
}

private class MediaCursorAdapter extends SimpleCursorAdapter{

public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[] { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[] { R.id.displayname, R.id.title, R.id.duration });
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
setContentView(R.layout.activity_main);
TextView title = (TextView)view.findViewById(R.id.title);
TextView name = (TextView)view.findViewById(R.id.displayname);
TextView duration = (TextView)view.findViewById(R.id.duration);

name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));

long durationInMs = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));

double durationInMin = ((double)durationInMs/1000.0)/60.0;

durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();

duration.setText("" + durationInMin);

view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.activity_main, parent, false);

bindView(v, context, cursor);

return v;
}
}

private View.OnClickListener onButtonClick = new View.OnClickListener() {

@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.play:
{
if(player.isPlaying())
{
handler.removeCallbacks(updatePositionRunnable);
player.pause();
playButton.setImageResource(android.R.drawable.ic_media_play);
}
else
{
if(isStarted)
{
player.start();
playButton.setImageResource(android.R.drawable.ic_media_pause);

updatePosition();
}
else
{
startPlay(currentFile);
}
}

break;
}
case R.id.next:
{
int seekto = player.getCurrentPosition() + STEP_VALUE;

if(seekto > player.getDuration())
seekto = player.getDuration();

player.pause();
player.seekTo(seekto);
player.start();

break;
}
case R.id.prev:
{
int seekto = player.getCurrentPosition() - STEP_VALUE;

if(seekto < 0)
seekto = 0;

player.pause();
player.seekTo(seekto);
player.start();

break;
}
}
}
};

private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
};

private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// returning false will call the OnCompletionListener
return false;
}
};

private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isMoveingSeekBar = false;
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isMoveingSeekBar = true;
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
if(isMoveingSeekBar)
{
player.seekTo(progress);

Log.i("OnSeekBarChangeListener","onProgressChanged");
}
}
};
}

这是我的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/dnp_1">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/displayname"
android:textSize="18sp"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/selectedfile"
android:text="Not file selected"
android:textColor="@android:color/black"
android:gravity="center_horizontal"
android:singleLine="true"
android:ellipsize="middle"/>

<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"/>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/screen_background_light"
android:padding="10dip">


<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar"
android:max="100"
android:paddingBottom="10dip"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:drawable/screen_background_light">

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prev"
android:src="@android:drawable/ic_media_previous"/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/play"
android:src="@android:drawable/ic_media_play"/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"
android:src="@android:drawable/ic_media_next"/>

</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="1.0"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/duration"
android:gravity="right"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="end"/>
</LinearLayout>
</LinearLayout>

</RelativeLayout>

这是我的 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.p.play"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.p.play.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MediaCursorAdapter"
android:label="@string/app_name">
</activity>
</application>

</manifest>

这是 Logcat

07-15 03:15:00.972: E/AndroidRuntime(1125): FATAL EXCEPTION: main
07-15 03:15:00.972: E/AndroidRuntime(1125): Process: com.p.play, PID: 1125
07-15 03:15:00.972: E/AndroidRuntime(1125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.p.play/com.p.play.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=1125, uid=10052 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.os.Looper.loop(Looper.java:136)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 03:15:00.972: E/AndroidRuntime(1125): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 03:15:00.972: E/AndroidRuntime(1125): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 03:15:00.972: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 03:15:00.972: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 03:15:00.972: E/AndroidRuntime(1125): at dalvik.system.NativeStart.main(Native Method)
07-15 03:15:00.972: E/AndroidRuntime(1125): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=1125, uid=10052 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.os.Parcel.readException(Parcel.java:1465)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.content.ContentResolver.query(ContentResolver.java:461)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.content.ContentResolver.query(ContentResolver.java:404)
07-15 03:15:00.972: E/AndroidRuntime(1125): at com.p.play.MainActivity.onCreate(MainActivity.java:65)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.Activity.performCreate(Activity.java:5231)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-15 03:15:00.972: E/AndroidRuntime(1125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-15 03:15:00.972: E/AndroidRuntime(1125): ... 11 more

请帮助我。提前致谢...

最佳答案

在 Android 应用程序的 AndroidManifest 文件中添加权限。即READ_EXTERNAL_STORAGE。只需在项目的 list 文件中添加此行

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

应用程序标签上方

关于java - 不幸的是应用程序已在 Eclipse 中停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24753497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com