gpt4 book ai didi

fragment 的 onCreate() 方法中的 Android 应用程序运行时错误

转载 作者:行者123 更新时间:2023-11-29 17:48:41 24 4
gpt4 key购买 nike

我正在为 Android 开发音乐播放器应用。我只使用 ViewPager 创建了一个包含 4 个 fragment 的 Activity 和空布局。到目前为止效果很好。

fragment 是艺术家、专辑、所有歌曲和播放列表。

现在,我试图用设备上所有歌曲的列表填充所有歌曲 fragment 。我为此添加了必要的代码,应用程序编译没有任何错误。但是,当我打开该应用程序时,我收到一条强制关闭消息“不幸的是,播放器已停止”。所以这是 logcat。

E/AndroidRuntime( 2539): FATAL EXCEPTION: main
E/AndroidRuntime( 2539): java.lang.NullPointerException
E/AndroidRuntime( 2539): at com.example.player.AllSongsFragment.onCreate(AllSongsFragment.java:26)
E/AndroidRuntime( 2539): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
E/AndroidRuntime( 2539): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
E/AndroidRuntime( 2539): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:834)
E/AndroidRuntime( 2539): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:841)
E/AndroidRuntime( 2539): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
E/AndroidRuntime( 2539): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
E/AndroidRuntime( 2539): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
E/AndroidRuntime( 2539): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
E/AndroidRuntime( 2539): at android.view.View.measure(View.java:15775)
E/AndroidRuntime( 2539): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
E/AndroidRuntime( 2539): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 2539): at android.view.View.measure(View.java:15775)
E/AndroidRuntime( 2539): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
E/AndroidRuntime( 2539): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime( 2539): at android.view.View.measure(View.java:15775)
E/AndroidRuntime( 2539): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)

所以,问题出在 AllSongsFragment 类的 onCreate 方法中的 NullPointerException

这里是AllSongsFragment.java的内容

package com.example.player;

import com.example.player.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.net.Uri;
import android.content.ContentResolver;
import android.database.Cursor;
import android.widget.ListView;
import com.example.player.adapter.AllSongsAdapter;
import com.example.player.Song;

public class AllSongsFragment extends Fragment {
private ArrayList<Song> songArray;
private ListView songListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
songListView = (ListView)getView().findViewById(R.id.all_songs_list);
songArray = new ArrayList<Song>();
getSongList();
// Sort the songs
Collections.sort(songArray, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
AllSongsAdapter songAdt = new AllSongsAdapter(getActivity(), songArray);
songListView.setAdapter(songAdt);
} // onCreate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.all_songs_fragment_layout, container, false);
return rootView;
} //onCreateView()

public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
// Iterate over the List
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);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songArray.add(new Song(thisId, thisTitle, thisArtist));
} while (musicCursor.moveToNext());
} // if
} //getSongList()
} //AllSongsFragment

我在 Song.java 中有一个 Song 类来表示每个歌曲对象。

package com.flairsome.player;

public class Song {
private long id;
private String title;
private String artist;
//Constructor
public Song(long songID, String songTitle, String songArtist) {
id=songID;
title=songTitle;
artist=songArtist;
}
// get methods
public long getID() {
return id;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
} //.Song

AllSongsAdapter.java 是

package com.example.player.adapter;

import android.widget.BaseAdapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.player.Song;
import com.example.player.R;

public class AllSongsAdapter extends BaseAdapter {
private ArrayList<Song> songs;
private LayoutInflater songInf;
// constructor
public AllSongsAdapter(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_item_layout, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
} //getView()
}

虽然我已经发布了大量代码,但从 logcat 来看,问题似乎出在 AllSongsFragment 类的 onCreate() 方法中。

我做错了什么吗?

如果您需要更多信息或我需要改进问题,请发表评论。谢谢。

最佳答案

移动这个

 songListView = (ListView)getView().findViewById(R.id.all_songs_list);

onActivityCreated

getView() 在您的案例中返回 null。 songListView 为 null,当您将适配器设置为 listview 结果时 NUllPointerException

您也可以将下面的内容移至 onActivityCreated

  songArray = new ArrayList<Song>();
getSongList();
// Sort the songs
Collections.sort(songArray, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
AllSongsAdapter songAdt = new AllSongsAdapter(getActivity(), songArray);
songListView.setAdapter(songAdt);

关于 fragment 的 onCreate() 方法中的 Android 应用程序运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24614277/

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