0

我一直试图在网上寻找答案,但一无所获。

所以这是我的问题:我在这里找到了一个有趣的教程,如何在 android 上开发自己的简单播放器,并开始将该代码集成到我的程序中,但遇到了视图问题。我什至无法理解它出现的原因:我尝试在任何地方插入 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);
    }
}

主要的.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

如果有人帮助我,我将不胜感激。先感谢您!

4

2 回答 2

1

您正在扩展一个 ListActivity,这要求您的布局中有一个 ListView 并带有 id @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"/>
于 2012-06-30T19:16:22.797 回答
0
 Your content must have a ListView whose id attribute is 'android.R.id.list'

如果您正在使用列表活动,您必须有一个 id 为“@android:id/list”的 listView

<?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>
于 2012-06-30T19:14:11.657 回答