片段不显示

问题描述:

我有一个活动,它有一个列表视图,它使用列表适配器来显示元素。当带有文本的元素被点击时,应该创建具有播放按钮,查找栏和媒体播放器的片段。但是,当我的活动被调用时,我得到这个错误。片段不显示

的线14是list_songs是具有 “片段” 开始标记

> 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 
所述一个

为活动的代码是

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> 

片段类是:

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

片段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> 
+1

堆栈跟踪是否会说更多?张贴完整的红色部分。另外,'name =='Play“'应该是'name.equals(”播放“)'。 – 2013-02-27 04:32:47

+0

你有没有在xml中给出片段的正确路径 – 2013-02-27 04:32:56

+0

什么是完整的堆栈跟踪? – alex 2013-02-27 04:33:02

首先,你给fill_parentListView的宽度和高度,这使得你的ListView占据整个父(这里LinearLayout),所以没有对Fragment地方。它很好,你有null检查你的代码if (frag == null),但你又试图将你的片段添加到不在屏幕上(未附加)的相同View

也许如果您发现Fragment未附加到您的视图层次结构中,您可能希望开始一项新活动。

if (frag == null) { 
    Intent playerActivity = new Intent(DisplaysongDetails.this, PlayerActivity.class) 
    startActivity(playerActivity); 
    } 

现在,创建一个新的名为ActivityPlayerActivity在其布局添加PlayerFragment。您可以使用Extras传递任何商品数据。

+0

在调整元素大小后,我终于可以解决这个问题。谢谢! – 2013-10-19 05:06:23

另一个问题:

seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar); 
seekBar.setOnTouchListener(this); 

seekBar只被定义为片段布局,而不是活动的一部分。对getActivity().findViewById()的调用应返回null,并在尝试添加监听器时导致NPE。但是,需要更多信息(来自堆栈跟踪)来查看是否有其他错误(除了这个和字符串比较)。

什么是有效的:

seekBar = (SeekBar)view.findViewById(R.id.seekBar); 
seekBar.setOnTouchListener(this); 
+0

(不知道如何格式化代码,尝试了4个空格)A - C,解决了我的问题。这是由于这个空指针异常。现在我的活动视图正在显示。但是,当我点击播放时,片段没有显示出来。另外,当我点击模拟器上的后退按钮时,Logcat会在我的片段中显示来自mediaplayer的一些输出 02-26 03:00:14.623:E/MediaPlayer 2629):在状态1停止呼叫 02-26 03:00:14.623:E/MediaPlayer(2629):错误(-38,0) 02-26 03:00:14。754:E/MediaPlayer(2629):错误(-38,0) – 2013-02-27 04:54:03

+0

彻底阅读媒体播放器文档。至于没有显示的片段,它可能是sujith在他的回答中提到的高度和宽度属性。只需使尺寸更小,以便片段和列表都适合屏幕 – 2013-02-27 05:06:20