2016-12-31 52 views
0

編輯新年快樂!準確地說,我在Android Studio中創建了一個簡單的音樂應用程序,用於從我的內部存儲器中讀取mp3文件。一切都好,直到我決定將一個隨機圖像放在列表上的每個單一軌道上。 (在我想要顯示一張圖像的歌曲名稱前面)。當我在ImageView的'src'中選擇一個單獨的圖像時,它可以正常工作並顯示給所有歌曲。我將atach下面我的代碼部分:主要活動:我的應用程序停止每當我加載它

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(); 
} 

活動主要的.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
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://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="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> 

在這旁邊,我已經2更多的java類。我認爲這個問題是在這裏,但IDK的什麼是錯的:

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]); 
} 

我的另一個疑惑是什麼「不贊成使用」的意思時,是我使用.getDrawable。 在此先感謝!

的logcat:

ime: FATAL EXCEPTION: main 
                    Process: com.alg.amzar.spectrum, PID: 28677 
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alg.amzar.spectrum/com.alg.amzar.spectrum.MainActivity}: java.lang.NullPointerException 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2433) 
                     at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346) 
                     at android.os.Handler.dispatchMessage(Handler.java:110) 
                     at android.os.Looper.loop(Looper.java:193) 
                     at android.app.ActivityThread.main(ActivityThread.java:5341) 
                     at java.lang.reflect.Method.invokeNative(Native Method) 
                     at java.lang.reflect.Method.invoke(Method.java:515) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646) 
                     at dalvik.system.NativeStart.main(Native Method) 
                    Caused by: java.lang.NullPointerException 
                     at com.alg.amzar.spectrum.MainActivity.schimbare(MainActivity.java:57) 
                     at com.alg.amzar.spectrum.MainActivity.onCreate(MainActivity.java:31) 
                     at android.app.Activity.performCreate(Activity.java:5343) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2433)  
                     at android.app.ActivityThread.access$800(ActivityThread.java:155)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)  
                     at android.os.Handler.dispatchMessage(Handler.java:110)  
                     at android.os.Looper.loop(Looper.java:193)  
                     at android.app.ActivityThread.main(ActivityThread.java:5341)  
                     at java.lang.reflect.Method.invokeNative(Native Method)  
                     at java.lang.reflect.Method.invoke(Method.java:515)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)  
                     at dalvik.system.NativeStart.main(Native Method)  

我的手機Android版本:4.4.2(API 19) 目標SDK版本: 「19」 MinSDK版本: 「14」

SongAdapter.java:

package com.alg.amzar.spectrum; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 



public class SongAdapter extends BaseAdapter { 

private ArrayList<Song> songs; 
private LayoutInflater songInf; 

public SongAdapter(Context c, ArrayList<Song> theSongs) { 
    songs = theSongs; 
    songInf = LayoutInflater.from(c); 
} 

@Override 
public int getCount() { 
    return songs.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    //map to song layout 
    LinearLayout songLay = (LinearLayout)songInf.inflate 
      (R.layout.song, parent, false); 
    //get title and artist views 
    TextView songView = (TextView)songLay.findViewById(R.id.song_title); 
    //get song using position 
    Song currSong = songs.get(position); 
    //get title and artist strings 
    songView.setText(currSong.getTitle()); 
    //set position as tag 
    songLay.setTag(position); 
    return songLay; 
} 

} 
+0

請也給手機的Android版本你與測試,並針對性版本的應用程序 –

+0

你應該調試你的應用程序,通過走行線,看看哪裏的問題。 – avinesh

回答

2

就你而言,findViewById(R.id.imagine)返回NULL,因爲它在setContentView()中提供的佈局「activity_main」中不存在。 findViewById()如果視圖在當前佈局中不存在,則返回NULL。 R.id.imagine存在於佈局song.xml中。

我想,你試圖用佈局'歌曲'來擴充ListView。只有在充氣後,纔可以撥打findViewByIdsetImageResource。你可以在類的SongAdapter中爲ListView中的每個元素在完成它們填充後完成它。

另一個建議是,不要使用隨機圖像,您可以從MediaMetadataRetriever類獲取歌曲的專輯封面。你可以參考android文檔。但是你必須先解決這個例外。

評論/刪除功能schimbare()在onCreate和編輯SongAdapter如下。

package com.alg.amzar.spectrum; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 



public class SongAdapter extends BaseAdapter { 

private ArrayList<Song> songs; 
private LayoutInflater songInf; 

int[] photos={R.drawable.img_0, R.drawable.img_1,R.drawable.img_2}; 
Random ran=new Random(); 
// CHECK THIS: 
public SongAdapter(Context c, ArrayList<Song> theSongs) { 
    songs = theSongs; 
    // CHECK THIS: 
    songInf = (LayoutInflater)c. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    return songs.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    //map to song layout 
    LinearLayout songLay = (LinearLayout)songInf.inflate 
      (R.layout.song, null); 
    //get title and artist views 
    TextView songView = (TextView)songLay.findViewById(R.id.song_title); 
    // CHECK THIS: 
    ImageView img = (ImageView)songLay.findViewById(R.id.imagine); 
    //get song using position 
    Song currSong = songs.get(position); 
    //get title and artist strings 
    songView.setText(currSong.getTitle()); 
    // CHECK THIS: 
    int i=ran.nextInt(photos.length); 
    img.setImageResource(photos[i]); 
    //set position as tag 
    songLay.setTag(position); 
    return songLay; 
} 

} 
+0

我無法正確理解在這種情況下要做什麼。首先,我想要使用MediaMetadatRetriever,但這對我來說似乎有點複雜 –

+0

您必須在SongAdapter類中調用'setImageResourse'。我會爲您提供一個鏈接:[鏈接](https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html)。請嘗試。 – 123rgt

+0

在嘗試MediaMetadataRetriever之前,您必須修復當前異常。這不是對當前異常的修復。 – 123rgt

0

棄用的意思是谷歌不再支持這種方法,他們告訴你有應該使用的NEWER方法。現在已棄用的方法正常工作。但有一天(也許明天也許是3年後)它將不再有效。

關於錯誤,請上傳日誌。

的錯誤是在第57行:

                 at com.alg.amzar.spectrum.MainActivity.schimbare(MainActivity.java:57)

我不知道在哪裏線57,但你可以檢查你的代碼,如果你仍然無法找到原因然後更新我們這行是57

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(); 
 

 
    // CHANGE photos.length to photos.length-1 
 
    int i=ran.nextInt(photos.length); 
 

 
    //ADD this 2 line of code below, then you can check in your Log if the problem is with the view (the view is null), or if the photos position is null. 
 

 
    Log.d("bug_tag",String.valueOf(photos[i])); 
 
    Log.d("bug_tag",String.valueOf(image.getId())); 
 

 

 
    image.setImageResource(photos[i]); 
 
}

+0

我添加了日誌 –

+0

我仍然無法將其配置出來。問題是我最初想到的。第57行:image.setImageResource(photos [i]); –

+0

查看我添加的代碼中的評論。 –

相關問題