마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

액티비티 화면 전환시 계속 튕겨서 질문드립니다.

0 추천

<string.xml>

<resources>
    <string name="app_name">노래목록</string>
    <string name="title1">사랑이 잘(with 오혁)</string>
    <string name="artist1">아이유(IU)</string>
    <string name="audio1">iu</string>
    <string name="song_image1">img_iu</string>
    <string name="time1">00:00:14</string>
</resources>

<MainActivity.java>

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("노래 목록");    }
    public void play(View v) {
        int id = v.getId();
        LinearLayout layout = (LinearLayout)findViewById(id);
        String tag = (String)layout.getTag();
        Intent it = new Intent(this, AudioImage.class);
        it.putExtra("it_tag", tag);
        startActivity(it);    }}

<activity_main.xml>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/audio1"
        android:tag="@string/audio1"
        android:clickable="true"
        android:onClick="play"
        android:background="@drawable/shape_list"     >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/title1"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="@string/artist1" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/time1" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<AudioImage.java>

public class AudioImage extends AppCompatActivity {
    MediaPlayer mp = new MediaPlayer();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_image);
        setTitle("노래 재생");
        Intent it = getIntent();
        String tag = it.getStringExtra("it_tag");
        TextView title = (TextView)findViewById(R.id.title);
        ImageView song_image = (ImageView)findViewById(R.id.song_image);
        Resources res = getResources();
        int stringId;
        String myKey;

        stringId = res.getIdentifier("title"+tag, "string", getPackageName());
        myKey = res.getString(stringId);
        title.setText(myKey);

        stringId = res.getIdentifier("song_image"+tag, "string", getPackageName());
        myKey = res.getString(stringId);

        int id_image = res.getIdentifier(myKey, "drawable", getPackageName());
        song_image.setImageResource(id_image);

        stringId = res.getIdentifier("audio"+tag, "string", getPackageName());
        myKey = res.getString(stringId);
        int id_audio = res.getIdentifier(myKey, "raw", getPackageName());
        mp = MediaPlayer.create(this, id_audio);
        mp.setLooping(false);
        mp.start();    }
    public void goBack(View v) {
        mp.stop();
        mp.release();
        finish();    }}

<audio_image.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:clickable="true"
    android:onClick="goBack"
    tools:context="com.example.jinhyukkim.report5_audio2.AudioImage">
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/shape_list"        />
    <ImageView
        android:id="@+id/song_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>

-------------------------------------------------------------------------

res-drawable 에 img_bigbang.jpg, img_iu.jpg, img_twice.jpg, shape_list.xml 이 있고

res-raw에 bigbang.mp4, iu.mp4, twice.mp4 가 있습니다.

 

하고자 하는 것은 처음 메인 액티비티에서 해당 레이아웃을 클릭하면 해당 레이아웃에 저장된 값을 가지고 audio_image.xml에 전달해서 audio액티비티에서 타이틀과 앨범 이미지가 뜨면서 노래가 재생되는 것이며, audio_image.xml에서 다시 아무대나 클릭(화면 전체가 레이아웃으로 지정되어 있으므로 레이아웃 클릭으로 봐도 무방)하면 다시 해당 액티비티가 종료되면서 메인화면이 뜨게 되는 것입니다.

값은 맞게 준거 같은데 Activity_main.xml의 화면에서 레이아웃 클릭하면 audio_image.xml화면이 떠야되는데 계속 강제적으로 종료되었다고 합니다...

얘기가 길지만....답변해 주시면 감사하겠습니다.

개발로 먹고살고싶은 초보 님이 2017년 4월 28일 질문
원래 각각의 3개의 이미자와 노래가 있는데 글 개재 글자수 제한으로 인해 두번째와 세번째는 어쩔수 없이 생략했습니다... 내용은 주어진 값만 다를 뿐 형식은 같습니다.

1개의 답변

0 추천
AndroidManifest.xml 에 새로운 Activity를 추가해주셨나요?
익명사용자 님이 2017년 4월 28일 답변
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AudioImage"></activity>
    </application>

</manifest>

매니페스트에 이렇게 두개 들어가면 맞지 않나요??
지금 쓰는 클래스가 MainActivity.class 랑 AudioImage.class 예요
로그캣으로 확인해 본 결과
AudioImage.class의 myKey = res.getString(stringId); 이 부분에서
04-29 16:29:41.611 19215-19215/com.example.jinhyukkim.report5_audio2 W/ResourceType: No package identifier when getting value for resource number 0x00000000
이렇게 로그캣이 뜨는데.... 이걸 봐도 뭐가 문제인지를 모르겠어요 ㅠㅠ
04-29 16:29:41.631 19215-19215/com.example.jinhyukkim.report5_audio2 D/AndroidRuntime: Shutting down VM
04-29 16:29:41.631 19215-19215/com.example.jinhyukkim.report5_audio2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jinhyukkim.report5_audio2, PID: 19215
stringId 값이 R.java에서 읽어오는 값이군요...바뀌었을 가능성이 있네요.
myKey값을 그냥 태그값으로 쓰세요
...