Reputation: 113
I am having an activity which has a list view which uses a list adapter to display elements. When the element which has text is clicked on the fragment should be created which has the play button, seek bar and has a media player. However I get this error when my activity is called.
The line 14 is the list_songs is the one which has "fragment" beginning tag
> 02-26 02:01:27.625: E/AndroidRuntime(2419): FATAL EXCEPTION: main
> 02-26 02:01:27.625: E/AndroidRuntime(2419):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.songs/com.songs.display.DisplaysongDetails}:
> android.view.InflateException: Binary XML file line #14: Error
> inflating class fragment
The code for the activity is
public class DisplaysongDetails extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.list_songs);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row,songDetails));
ListView listView = (ListView) this.findViewById(android.R.id.list);//getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = ((TextView) view).getText().toString();
if (name=="Play")
{
PlayerFragment frag = (PlayerFragment) getFragmentManager().findFragmentById(R.id.player_fragment);
if (frag == null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.player_fragment, new PlayerFragment());
ft.commit();
}}
}
});
}}
list_songs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<fragment
android:id="@+id/player_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.songs.PlayerFragment" />
</LinearLayout>
The fragment class is:
public class PlayerFragment extends Fragment implements OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private Button btn_play;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
private int length=0;
String URL = (new AppConfig()).getURL();
private final String url = URL + "/play/song.mp3";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() {
public void run() {
updateSeekProgress();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("NewApi")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.player_fragment, container, false);
btn_play = (Button) view.findViewById(R.id.btn_play);
btn_play.setOnClickListener(new OnClickListener());
seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
return view;
}}
Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_play"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="play" />
</LinearLayout>
</LinearLayout>
Upvotes: 0
Views: 335
Reputation: 2421
First, you have given fill_parent
for both width and height of your ListView
which makes your ListView occupy the whole parent(here LinearLayout
), so no place for Fragment
. Its good that you have null
check in your code if (frag == null)
, but again you are trying to add your fragment to the same View
which is not in the screen(not attached).
Perhaps you would want to start a new activity if you found that your Fragment
is not attached to your view hierarchy.
if (frag == null) {
Intent playerActivity = new Intent(DisplaysongDetails.this, PlayerActivity.class)
startActivity(playerActivity);
}
Now, create a new Activity
called PlayerActivity
and add PlayerFragment
in its layout. You can pass any item data using Extras
.
Upvotes: 1
Reputation: 36449
Another issue:
seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
seekBar
is only defined as part of the Fragment layout, not the Activity's. That call to getActivity().findViewById()
should return null
and result in an NPE when trying to add the Listener. However, more info (from the stack trace) is needed to see if anything else is wrong (besides this and the String comparison).
What would be valid:
seekBar = (SeekBar)view.findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
Upvotes: 1