我的应用程序每次加载时都会停止

时间:2016-12-31 12:15:31

标签: java android random

编辑新年快乐!准确地说,我在Android Studio中创建了一个简单的音乐应用程序,用于从内部存储中读取mp3文件。一切都很好,直到我决定在列表中的每个轨道上放置一个随机图像。 (在我希望出现一个图像的歌曲名称前面。)当我在ImageView的'src'中选择一个单独的图像时,它适用于所有歌曲。我将在下面提供我的部分代码:主要活动:

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>

除此之外,我还有两个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) 

我的手机安卓版: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;
}

}

2 个答案:

答案 0 :(得分:2)

在您的情况下,"code": "StorageAccountAlreadyExists", "message": "The storage account named mystorageaccountname already exists under the subscription." 返回NULL,因为它在findViewById(R.id.imagine)中提供的布局“activity_main”中不存在。如果当前布局中不存在该视图,则setContentView()将返回NULL。 R.id.imagine存在于布局song.xml中。

我认为,您正在尝试使用布局“歌曲”来扩充ListView。只有在充气后,您才能拨打findViewById()findViewById。您可以在类SongAdapter中为ListView中的每个元素充气后完成它。

另一个建议是,您可以从MediaMetadataRetriever类获取歌曲的专辑封面,而不是使用随机图像。你可以参考android文档。但是你必须先解决这个例外。

在onCreate中评论/删除功能setImageResource并编辑SongAdapter,如下所示。

schimbare()

答案 1 :(得分:0)

不推荐意味着Google不再支持此方法,他们告诉您应该使用NEWER方法。已弃用的方法现在正常运行。但是有一天(也许明天可能是今天的3年)它将不再有效。

关于错误,请上传日志。

错误在第57行:

&#13;
&#13;
                                                                        at com.alg.amzar.spectrum.MainActivity.schimbare(MainActivity.java:57)
&#13;
&#13;
&#13;

我不确定第57行是哪里但您可以在代码中查看,如果您仍然无法找到原因,请更新我们哪行是57

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