Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android活动只有在';s声明为主要和启动器活动_Java_Android_Android Intent_Android Manifest_Intentfilter - Fatal编程技术网

Java Android活动只有在';s声明为主要和启动器活动

Java Android活动只有在';s声明为主要和启动器活动,java,android,android-intent,android-manifest,intentfilter,Java,Android,Android Intent,Android Manifest,Intentfilter,我正在用android制作一个音乐播放器。我正在尝试的是显示播放列表 当应用程序启动并用户点击列表项时,意图将传递给播放歌曲的android_播放器活动。 但它不工作,当我点击列表项时,应用程序崩溃并关闭 但当我将music_player活动声明为启动器时,它就起作用了。但我想先显示播放列表,而不是音乐播放器 我有以下清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schem

我正在用android制作一个音乐播放器。我正在尝试的是显示播放列表 当应用程序启动并用户点击列表项时,意图将传递给播放歌曲的android_播放器活动。 但它不工作,当我点击列表项时,应用程序崩溃并关闭

但当我将music_player活动声明为启动器时,它就起作用了。但我想先显示播放列表,而不是音乐播放器

我有以下清单文件

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
   <activity
        android:name=".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=".AndroidMusicPlayerActivity"
        android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation"
        android:screenOrientation="portrait">
        <-- <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter> -->
   </activity>
</application>
显示播放列表的MainActivity传递以下意图

 lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listItem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidMusicPlayerActivity.class);
                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);
                setResult(100, in);
                // Closing PlayListView
                finish();
            }
        });
我应该做什么样的更改才能使代码正常工作。 我不熟悉安卓系统。请帮忙。
谢谢

为什么要将这两个活动都作为清单中的启动器?删除它,然后检查。

我可能遗漏了一些内容,但在itemClickListener中,您似乎在第一个活动中调用finish(),而没有启动第二个活动。
当然,您需要添加startActivity(in);打电话

“它崩溃并关闭”->发布你的logcat并指出它引用的代码段中的哪一行。请也发布logcat和你的java代码…@Class Stacker添加了logcat数据我们需要崩溃时的logcat输出,我的朋友。04-30 16:25:53.920:W/IInputConnectionWrapper(4504):showStatusIcon on inactive InputConnection 04-30 16:25:54.030:D/OpenGLRenderer(4504):刷新缓存(模式1)@ClassStacker这就是日志中的全部内容。我只将一个主活动作为启动器活动。但主要活动不是启动PlayerActivity。您正在lv中调用set-result方法。setOnItemClickListener(new OnItemClickListener()}方法调用startActivity();方法并传递意图。当我使用startActivity(in)时,它启动了第二个活动,但它没有播放预期的歌曲。当我使用startActivity(in)时;它启动了第二个活动,但没有播放预期的歌曲。在我看来,您正在尝试反向使用onActivityResult。如果您将'currentSongIndex=data.getExtras().getInt(“songIndex”);//播放选定的歌曲播放歌曲(currentSongIndex);'在第二个活动的oncreate中,我认为它可以满足您的需要。您的方法中的编码是什么//播放选定的歌曲playSong(currentSongIndex);只创建MediaPlayer的一个对象并创建它。然后调用MediaPlayer.start()在您切换到另一个活动或您破坏了您的应用程序后,它不会停止事件activiy@user2145222我在第二个活动的onCreate()方法和提取的songindex中使用了Bundle extras=getIntent().getExtras();现在它可以正常工作了。我需要onActivityResult()吗方法,否则我应该将其删除?根据您所说的,是的,我将删除此代码,因为我认为它与您想要的内容无关。如果您只想传递简单的值,请在新活动的oncreate或onstart方法中读取这些值,并将其原样添加。做得很好。
 lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listItem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidMusicPlayerActivity.class);
                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);
                setResult(100, in);
                // Closing PlayListView
                finish();
            }
        });
04-30 16:17:16.946: D/OpenGLRenderer(3858): Enabling debug mode 0 04-30
16:17:29.958: W/IInputConnectionWrapper(3858): showStatusIcon on inactive InputConnection 
04-30 16:17:30.018: D/OpenGLRenderer(3858): Flushing caches (mode 0)