samedi 31 décembre 2016

My app stopped every time when I load it

To be precise ,I created a simple music app in Android Studio who read mp3 files from my internal storage. All good until I decide to put one random image on every single track on the list. (In front of the name of the song I want to appear one image).When I select one single image in 'src' at ImageView ,it works and appear to all songs. I will atach a part of my code below: Main Activity:

package com.alg.amzar.spectrum;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

static {
    System.loadLibrary("native-lib");
}
private ArrayList<Song> songList;
private ListView songView;


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

    schimbare();

    songView = (ListView)findViewById(R.id.song_list);
    songList = new ArrayList<Song>();

    getSongList();

    Collections.sort(songList, new Comparator<Song>(){
        public int compare(Song a, Song b){
            return a.getTitle().compareTo(b.getTitle());
        }
    });

    SongAdapter songAdt = new SongAdapter(this, songList);
    songView.setAdapter(songAdt);


}

public void schimbare() {
    int[] photos={R.drawable.img_0, R.drawable.img_1,R.drawable.img_2};

    ImageView image = (ImageView) findViewById(R.id.imagine);

    Random ran=new Random();
    int i=ran.nextInt(photos.length);
    image.setImageResource(photos[i]);
}

public void getSongList() {
    ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            songList.add(new Song(thisId, thisTitle));
        }
        while (musicCursor.moveToNext());
    }
}

public native String stringFromJNI();
}

Activity Main .xml:

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF330000"
tools:context=".MainActivity" >

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

</LinearLayout>

Song.xml :

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="songPicked"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
    android:layout_width="70dp"
    android:id="@+id/imagine"
    android:layout_height="50dp" />

<TextView
    android:id="@+id/song_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF99"
    android:textSize="15sp"
    android:textStyle="bold"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="75dp"
    android:layout_marginTop="-42dp"/>


</LinearLayout>

Beside this ,I've 2 more java classes. I think the problem is here ,but idk what is wrong:

public void schimbare() {
    int[] photos={R.drawable.img_0, R.drawable.img_1,R.drawable.img_2};

    ImageView image = (ImageView) findViewById(R.id.imagine);

    Random ran=new Random();
    int i=ran.nextInt(photos.length);
    image.setImageResource(photos[i]);
}

Another wondering of mine is what 'deprecated' means when is I use .getDrawable. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire