2013-02-27 4 views
0

요소를 표시하는 데 목록 어댑터를 사용하는 목록보기가있는 활동이 있습니다. 프래그먼트에서 텍스트가있는 요소를 클릭하면 재생 버튼이 있고 바를 찾고 미디어 플레이어가 있어야합니다. 그러나 내 활동이 호출되면이 오류가 발생합니다. 조각이 표시되지 않습니다.

라인 14

가 "단편"시작 태그

> 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 
을 갖는 하나 list_songs이다

활동에 대한 코드가

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 ("Play")'이어야합니다. –

+0

xml의 단편에 올바른 경로를 지정했습니다. –

+0

전체 스택 추적은 무엇입니까? – alex

답변

1

첫째, 당신은 당신의 ListView가, (여기 LinearLayout) Fragment에 대한 그래서 장소를 전체 부모를 차지할 수 있습니다 귀하의 ListView의 폭과 높이를 모두를위한 fill_parent을 주었다. null 코드가 if (frag == null) 인 것을 확인했으나 화면에없는 동일한 View에 단편을 추가하려고합니다 (첨부되지 않음).

아마도 Fragment이보기 계층에 연결되어 있지 않다면 새로운 활동을 시작하고 싶을 것입니다.

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

지금, 새로운 ActivityPlayerActivity라는 만들고 레이아웃에 PlayerFragment를 추가합니다. Extras을 사용하여 항목 데이터를 전달할 수 있습니다.

+0

요소의 크기를 조정 한 후 마침내이를 해결할 수있었습니다. 감사! –

1

또 다른 문제 :

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

seekBar에만 조각 레이아웃이 아닌 활동의 부분으로 정의된다. getActivity().findViewById()을 호출하면 null이 반환되고 Listener를 추가 할 때 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) –

+0

미디어 플레이어 설명서를 자세히 읽습니다. 보여주지 않는 조각에 관해서는 sujith가 그의 대답에서 말하는 높이와 너비 속성 일 것입니다. 프래그먼트와리스트가 모두 화면에 맞도록 크기를 작게 만듭시다. –

관련 문제