2016-12-31 61 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

查看我添加的代码中的评论。 –

相关问题