gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:08:29 27 4
gpt4 key购买 nike

编辑新年快乐!准确地说,我在 Android Studio 中创建了一个简单的音乐应用程序,它从我的内部存储中读取 mp3 文件。一切都很好,直到我决定在列表中的每个轨道上放置一张随机图像。 (我想在歌曲名称前面显示一张图像)。当我在 ImageView 的“src”中选择一张图像时,它会起作用并出现在所有歌曲中。我将在下面附上我的部分代码:主要 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 主要.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>

歌曲.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 类。我认为问题出在这里,但不知道出了什么问题:

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 时“已弃用”是什么意思。提前致谢!

日志猫:

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

}

最佳答案

在您的情况下,findViewById(R.id.imagine) 返回 NULL,因为它不存在于您在 setContentView() 中提供的布局“activity_main”中>。如果当前布局中不存在该 View ,findViewById() 将返回 NULL。 R.id.imagine 存在于布局 Song.xml 中。

我认为,您正在尝试使用布局“歌曲”来膨胀 ListView。只有在inflate之后你才能调用findViewByIdsetImageResource。您可以在类 SongAdapter 中完成此操作,对 ListView 中的每个元素进行膨胀后。

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

在onCreate中注释/删除函数schimbare()并编辑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;
}

}

关于java - 我的应用程序每次加载时都会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408208/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com