我正在开发片段音乐播放器的应用程序。在这里,当我使用代码从XML获取视图时,它停止了工作。
我的xml代码是..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutMusicList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="@drawable/default_album_art_thumb"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewMusic"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:color/transparent" />
<LinearLayout
android:id="@+id/linearLayoutPlayingSong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:gravity="center"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageViewAlbumArt"
android:layout_width="@dimen/small_image_art_width"
android:layout_height="@dimen/small_image_art_width" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textNowPlaying"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:gravity="center_horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/empty_text"
android:textColor="@color/white"
android:textSize="15dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/btnPrevious"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_action_previous" />
<Button
android:id="@+id/btnPlay"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_action_play"
android:visibility="gone" />
<Button
android:id="@+id/btnPause"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_action_pause" />
<Button
android:id="@+id/btnStop"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_action_stop" />
<Button
android:id="@+id/btnNext"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_action_next" />
<Button
android:id="@+id/btnMusicPlayer"
android:layout_width="@dimen/small_button_width"
android:layout_height="@dimen/small_button_width"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_music" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textBufferDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white" />
<TextView
android:id="@+id/textDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="@color/white" />
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
片段代码是..
public class Device_Main extends Fragment {
Device_CustomAdapter deviceCustomAdapter = null;
static TextView playingSong;
Button btnPlayer;
static Button btnPause, btnPlay, btnNext, btnPrevious;
Button btnStop;
LinearLayout mediaLayout;
static LinearLayout linearLayoutPlayingSong;
ListView mediaListView;
ProgressBar progressBar;
TextView textBufferDuration, textDuration;
static ImageView imageViewAlbumArt;
static Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.device_activity_main, container, false);
context = Device_Main.this.getActivity();
init();
//<--------------------------------------------------->>>>>>>>>>>>>.
return view;
}
private void init() {
getViews();
setListeners();
playingSong.setSelected(true);
progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.orange), PorterDuff.Mode.SRC_IN);
if(Device_PlayerConstants.SONGS_LIST.size() <= 0){
Device_PlayerConstants.SONGS_LIST = Device_UtilFunctions.listOfSongs(getActivity().getApplicationContext());
}
setListItems();
}
private void setListItems() {
deviceCustomAdapter = new Device_CustomAdapter(this,R.layout.device_custom_list, Device_PlayerConstants.SONGS_LIST);
mediaListView.setAdapter(deviceCustomAdapter);
mediaListView.setFastScrollEnabled(true);
}
private void getViews() {
playingSong = (TextView) getView().findViewById(R.id.textNowPlaying);
btnPlayer = (Button) getView().findViewById(R.id.btnMusicPlayer);
mediaListView = (ListView) getView().findViewById(R.id.listViewMusic);
mediaLayout = (LinearLayout) getView().findViewById(R.id.linearLayoutMusicList);
btnPause = (Button) getView().findViewById(R.id.btnPause);
btnPlay = (Button) getView().findViewById(R.id.btnPlay);
linearLayoutPlayingSong = (LinearLayout) getView().findViewById(R.id.linearLayoutPlayingSong);
progressBar = (ProgressBar) getView().findViewById(R.id.progressBar);
btnStop = (Button) getView().findViewById(R.id.btnStop);
textBufferDuration = (TextView) getView().findViewById(R.id.textBufferDuration);
textDuration = (TextView) getView().findViewById(R.id.textDuration);
imageViewAlbumArt = (ImageView) getView().findViewById(R.id.imageViewAlbumArt);
btnNext = (Button) getView().findViewById(R.id.btnNext);
btnPrevious = (Button) getView().findViewById(R.id.btnPrevious);
}
private void setListeners() {
mediaListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View item, int position, long id){
Log.d("TAG", "TAG Tapped INOUT(IN)");
Device_PlayerConstants.SONG_PAUSED = false;
Device_PlayerConstants.SONG_NUMBER = position;
boolean isServiceRunning = Device_UtilFunctions.isServiceRunning(Device_SongService.class.getName(), getActivity().getApplicationContext());
if (!isServiceRunning) {
Intent i = new Intent(getActivity().getApplicationContext(),Device_SongService.class);
context.startService(i);
} else {
Device_PlayerConstants.SONG_CHANGE_HANDLER.sendMessage(Device_PlayerConstants.SONG_CHANGE_HANDLER.obtainMessage());
}
updateUI();
changeButton();
Log.d("TAG", "TAG Tapped INOUT(OUT)");
}
});
btnPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Device_Main.this.getActivity(),Device_AudioPlayerActivity.class);
startActivity(i);
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Device_Controls.playControl(getActivity().getApplicationContext());
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Device_Controls.pauseControl(getActivity().getApplicationContext());
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Device_Controls.nextControl(getActivity().getApplicationContext());
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Device_Controls.previousControl(getActivity().getApplicationContext());
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity().getApplicationContext(), Device_SongService.class);
context.stopService(i);
linearLayoutPlayingSong.setVisibility(View.GONE);
}
});
imageViewAlbumArt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Device_Main.this.getActivity(),Device_AudioPlayerActivity.class);
startActivity(i);
}
});
}
@Override
public void onResume() {
super.onResume();
try{
boolean isServiceRunning = Device_UtilFunctions.isServiceRunning(Device_SongService.class.getName(), getActivity().getApplicationContext());
if (isServiceRunning) {
updateUI();
}else{
linearLayoutPlayingSong.setVisibility(View.GONE);
}
changeButton();
Device_PlayerConstants.PROGRESSBAR_HANDLER = new Handler(){
@Override
public void handleMessage(Message msg){
Integer i[] = (Integer[])msg.obj;
textBufferDuration.setText(Device_UtilFunctions.getDuration(i[0]));
textDuration.setText(Device_UtilFunctions.getDuration(i[1]));
progressBar.setProgress(i[2]);
}
};
}catch(Exception e){}
}
@SuppressWarnings("deprecation")
public static void updateUI() {
try{
Device_MediaItem data = Device_PlayerConstants.SONGS_LIST.get(Device_PlayerConstants.SONG_NUMBER);
playingSong.setText(data.getTitle() + " " + data.getArtist() + "-" + data.getAlbum());
Bitmap albumArt = Device_UtilFunctions.getAlbumart(context, data.getAlbumId());
if(albumArt != null){
imageViewAlbumArt.setBackgroundDrawable(new BitmapDrawable(albumArt));
}else{
imageViewAlbumArt.setBackgroundDrawable(new BitmapDrawable(Device_UtilFunctions.getDefaultAlbumArt(context)));
}
linearLayoutPlayingSong.setVisibility(View.VISIBLE);
}catch(Exception e){}
}
public static void changeButton() {
if(Device_PlayerConstants.SONG_PAUSED){
btnPause.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
}else{
btnPause.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.GONE);
}
}
public static void changeUI(){
updateUI();
changeButton();
}
}
Log Catt .. ..
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.jithin.myapplication.Device_Main.getViews(Device_Main.java:83)
at com.example.jithin.myapplication.Device_Main.init(Device_Main.java:66)
at com.example.jithin.myapplication.Device_Main.onCreateView(Device_Main.java:58)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
任何人都可以帮我找到这个错误的解决方案..谢谢......
答案 0 :(得分:3)
在创建$wizzy->getConfig()->shouldReceive('get')->once()
->with('wizzy.prefix', '')->andReturn($this->defaults['prefix']);
之前,您正在调用getView()
方法。移动View
中的init()
方法。
答案 1 :(得分:1)
您的片段getView()方法将为null,因为在初始化这些视图之前,OnCreate上的视图未返回到活动。
首先将视图对象传递给init函数。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.device_activity_main, container, false);
context = Device_Main.this.getActivity();
init(view);
return view;
}
现在将视图对象从init函数传递给getViews()方法
private void init(View view) {
getViews(view);
setListeners();
playingSong.setSelected(true);
progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.orange), PorterDuff.Mode.SRC_IN);
if(Device_PlayerConstants.SONGS_LIST.size() <= 0){
Device_PlayerConstants.SONGS_LIST = Device_UtilFunctions.listOfSongs(getActivity().getApplicationContext());
}
setListItems();
}
现在使用view对象来使用findViewById()
private void getViews(View view) {
playingSong = (TextView) view.findViewById(R.id.textNowPlaying);
btnPlayer = (Button) view.findViewById(R.id.btnMusicPlayer);
答案 2 :(得分:1)
getView()
仍为null
,直到return view;
onCreateView()
为止init();
由于您getViews();
拨打getView()
,return view;
会在context = Device_Main.this.getActivity();
init();
之前拨打onActivityCreated()
,因此会生成NPE。
解决方案可以是移动此行
batch_factory
进入roo
答案 3 :(得分:1)
将视图传递给init()方法(例如:init(view))并从那里传递给getViews()方法(例如:getViews(view))。
在getViews()代码中使用该视图,
playingSong = (TextView) view.findViewById(R.id.textNowPlaying);
答案 4 :(得分:1)
您正在从onCreateView()调用init(),而onCreateView()必须只包含用于为Fragment扩展视图布局的代码。此时getView()方法将返回null,因为它未初始化。
您必须将所有初始化代码(init())移动到片段生命周期中稍后调用的方法,即onActivityCreated()。
请参阅:https://developer.android.com/guide/components/fragments.html了解片段生命周期。