2012-06-30 144 views
0

我一直在試圖找到一個衝浪互聯網的答案,但什麼都沒發現。Missing ListView

所以這裏是我的問題:我發現了一個有趣的教程,如何在android here上開發自己的簡單播放器,並開始將該代碼集成到我的程序中,但是面對Views的問題。我甚至不明白它爲什麼會出現:我試圖在任何地方插入ListView,但沒有發生任何事情。因此,這裏是我的代碼:

EasyFlippingActivity.java

package my.easyflipping; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ViewFlipper; 

public class EasyflippingActivity extends ListActivity { 

    private ViewFlipper flipper; 
    private static final String MEDIA_PATH = new String("/sdcard/"); 
    private List<String> songs = new ArrayList<String>(); 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.flipper = (ViewFlipper)findViewById(R.id.flipper); 
     Button groups; 
     (groups = (Button)findViewById(R.id.groups)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.group)); 
     Button songs; 
     (songs = (Button)findViewById(R.id.songs)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.songs)); 
     groups.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       im_going_left(); 
       flipper.showNext(); 
       groups_func(); 
      } 
      }); 
     songs.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       update_song_list(); 
       im_going_right(); 
       flipper.showNext(); 
       songs_func(); 
      } 
     }); 
    } 

    public void update_song_list() { 
     File home = new File(MEDIA_PATH); 
     if (home.listFiles(new Mp3Filter()).length > 0) { 
       for (File file : home.listFiles(new Mp3Filter())) { 
         songs.add(file.getName()); 
       } 

       ArrayAdapter<String> songList = new ArrayAdapter<String>(this, 
           R.layout.song_item, songs); 
       setListAdapter(songList); 
     } 
} 


    public void songs_func() 
    { 
     Button back; 

     back = (Button)findViewById(R.id.back); 
     back.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       im_going_left(); 
       flipper.showPrevious(); 
       return; 

      } 
     }); 
    } 

    public void groups_func() 
    { 
     Button back; 

     back = (Button)findViewById(R.id.back); 
     back.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       im_going_right(); 
       flipper.showPrevious(); 
       return; 

      } 
     }); 
    } 

    public void im_going_right() 
    { 
     Animation s_in = AnimationUtils.loadAnimation(this, R.layout.slideinleft); 
     Animation s_out = AnimationUtils.loadAnimation(this, R.layout.slideoutright); 
     this.flipper.setInAnimation(s_in); 
     this.flipper.setOutAnimation(s_out); 
    } 

    public void im_going_left() 
    { 
     Animation s_in = AnimationUtils.loadAnimation(this, R.layout.slidein); 
     Animation s_out = AnimationUtils.loadAnimation(this, R.layout.slideout); 
     this.flipper.setInAnimation(s_in); 
     this.flipper.setOutAnimation(s_out); 
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flipper" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 


    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="80dp" 
     android:src="@drawable/phones" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="400dp" > 

     <Button 
      android:id="@+id/groups" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="45dp" 
      android:text="" /> 

     <Button 
      android:id="@+id/songs" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="45dp" 
      android:text="" /> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/groups" 
      android:layout_below="@+id/groups" 
      android:src="@drawable/groupstext" /> 

     <ImageView 
      android:id="@+id/imageView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/imageView2" 
      android:layout_alignRight="@+id/songs" 
      android:src="@drawable/songstext" /> 
    </RelativeLayout> 

    </LinearLayout> 


    <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="20dip" 
      android:layout_gravity="top" 
      android:layout_marginTop="50dip"> 
     <ListView android:id="@+id/list" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:drawSelectorOnTop="false"/> 

     <TextView android:id="@+id/empty" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="No songs found on SD Card."/> 
     <Button 
      android:id="@+id/back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="45dp" 
      android:text="back" /> 


    </LinearLayout> 

</ViewFlipper> 

Mp3Filter.java

package my.easyflipping; 

import java.io.File; 
import java.io.FilenameFilter; 

public class Mp3Filter implements FilenameFilter { 

    public boolean accept(File arg0, String name) { 
     return (name.endsWith(".mp3")); 
    } 

} 

song_item.xml

<?xml version="1.0" encoding="utf-8"?> 
    <TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

日誌和錯誤日誌:

06-30 18:54:56.314: D/dalvikvm(189): GC freed 735 objects/55256 bytes in 70ms 
06-30 18:54:56.354: D/AndroidRuntime(189): Shutting down VM 
06-30 18:54:56.354: W/dalvikvm(189): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
06-30 18:54:56.354: E/AndroidRuntime(189): Uncaught handler: thread main exiting due to uncaught exception 
06-30 18:54:56.364: E/AndroidRuntime(189): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.easyflipping/my.easyflipping.EasyflippingActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.os.Looper.loop(Looper.java:123) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ActivityThread.main(ActivityThread.java:4363) 
06-30 18:54:56.364: E/AndroidRuntime(189): at java.lang.reflect.Method.invokeNative(Native Method) 
06-30 18:54:56.364: E/AndroidRuntime(189): at java.lang.reflect.Method.invoke(Method.java:521) 
06-30 18:54:56.364: E/AndroidRuntime(189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
06-30 18:54:56.364: E/AndroidRuntime(189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
06-30 18:54:56.364: E/AndroidRuntime(189): at dalvik.system.NativeStart.main(Native Method) 
06-30 18:54:56.364: E/AndroidRuntime(189): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ListActivity.onContentChanged(ListActivity.java:236) 
06-30 18:54:56.364: E/AndroidRuntime(189): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.Activity.setContentView(Activity.java:1622) 
06-30 18:54:56.364: E/AndroidRuntime(189): at my.easyflipping.EasyflippingActivity.onCreate(EasyflippingActivity.java:26) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
06-30 18:54:56.364: E/AndroidRuntime(189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
06-30 18:54:56.364: E/AndroidRuntime(189): ... 11 more 
06-30 18:54:56.384: I/dalvikvm(189): threadid=7: reacting to signal 3 
06-30 18:54:56.384: E/dalvikvm(189): Unable to open stack trace file '/data/anr/traces.txt': Permission denied 
06-30 18:58:28.214: I/Process(189): Sending signal. PID: 189 SIG: 9 

我會很感激,如果有人幫助我。先謝謝你!

+1

要擴展一個ListActivity,你**必須**已經與ID'佈局一個ListView @android:ID/list'。 – Sam

+0

我擁有它,但僅限於我的腳蹼的第二個佈局。所以我的問題是如何讓系統看到它。 – user1493420

回答

0
Your content must have a ListView whose id attribute is 'android.R.id.list' 

,如果你使用的是列表activiyt你必須有一個ID爲ListView控件 「@android:ID /列表」

<?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」 
> 
<TextView 
android:layout_width=」fill_parent」 
android:layout_height=」wrap_content」 
android:text=」 「 
android:id=」@+id/selection」 
/> 
<ListView 
android:id=」@android:id/list」 
android:choiceMode=」multipleChoice」 
android:layout_width=」fill_parent」 
android:layout_height=」wrap_content」/> 
</LinearLayout> 
+0

http://maohao.wordpress.com/2009/11/26/listview-and-listactivity-demo/ –

1

要擴展一個ListActivity,這需要,你有一個ListView在您的佈局中,編號爲@android:id/list。 main.xml中嘗試改變這一點:

<ListView android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 

這樣:

<ListView android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 
+0

+1我認爲你是對的! :) –

+0

謝謝!完成,它的工作。 – user1493420

+0

@ user1493420很高興能幫到你!請點擊答案左上角的複選標記,表示此問題已解決。 – Sam