0

我正在尝试制作一个下拉导航菜单,这样我就可以使用 SherlockFragmentActivity 和 SerlockListFragment 在我的页面之间移动,但是每次我启动我的应用程序时都会出现以下错误:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.download.manager/com.download.manager.Main}:java.lang.RuntimeException:您的内容必须有一个 id 属性为 android.R.id.list 的 ListView

这是 SherlockFragmentActivity 类

public class Main extends SherlockFragmentActivity implements OnNavigationListener{

private static final String KEY_MODELS="models";
private static final String KEY_POSITION="position";
private static final String[] labels= { "All", "Downloads","Completed","Later" };
private CharSequence[] models=new CharSequence[4];
private DownloadList frag=null;
private int lastPosition=-1;  

ActionBar act;
ViewPager myviewpager;
int mSelectedPageIndex=1;
DownloadService downloadservice;
Intent serviceIntent ;


@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    
    frag=(DownloadList)getSupportFragmentManager().findFragmentById(android.R.id.content);
    if (frag==null) {
        frag=new DownloadList();
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
      }

    if (state != null) {
        models=state.getCharSequenceArray(KEY_MODELS);
      }
    
    if (downloadservice==null)
        downloadservice= new DownloadService();
    ArrayAdapter<String> nav=null;
    act=getSupportActionBar();
   
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    {
      nav= new ArrayAdapter<String>( act.getThemedContext(),android.R.layout.simple_spinner_item,labels);
    }
    else 
    {
      nav=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,labels);
    }
    
    nav.setDropDownViewResource(android.R.layout.simple_list_item_1);
    act.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    act.setDisplayShowTitleEnabled(false);
    act.setHomeButtonEnabled(false);
    
    act.setListNavigationCallbacks(nav, this);
    if (state != null) {
        act.setSelectedNavigationItem(state.getInt(KEY_POSITION));
      }
   
    serviceIntent= new Intent(Main.this,downloadservice.getClass());
    if (startService(serviceIntent)==null)
        startService(serviceIntent);
    
    act.setTitle("");
   
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater infalter = getSupportMenuInflater();
    infalter.inflate(R.menu.mymenu, menu);
    
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.sub1)
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Downlaod New File");
        alert.setMessage("Enter URL");
        final EditText input = new EditText(this);
        alert.setView(input);
input.setText("http://205.196.123.184/ddpha7c5b8lg/616c36j0d1xbztf   /Lecture+ppt+Ch2.ppt");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString();
                Log.d("value",value);
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });

        alert.show();
    }   
        
    return super.onOptionsItemSelected(item);
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    lastPosition=itemPosition;
    
    
    
    return false;
}

这是 SherlockListFragment 类

public  class DownloadList extends SherlockListFragment {
int index=0;
Button downloadButton;
Button pauseButton;
Button resumeButton;
TextView textLink;
TextView progressbar;
DownloadService downloadservice= new DownloadService();
MyAdapter listadapter;
Intent serviceIntent ;
String state;


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getActivity().setContentView(R.layout.downloadlist);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup          
 container,BundlesavedInstanceState) {
    View rootView = inflater.inflate(R.layout.downloadlist, container, false);
    
    
    
    return rootView;
}
int getIndex()
{
    return index;
}
public class MyAdapter extends ArrayAdapter<NewDownload>
{
     private final List<NewDownload> list;
      private final Activity context;
      Thread t;
      
    public MyAdapter(Context context,List<NewDownload> list) {
        super(context, R.layout.list_item,  list);
        this.context = (Activity) context;
        this.list = list;
    }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getActivity().
                                          getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_item, null);

            pauseButton = (Button) row.findViewById(R.id.btn1);
            resumeButton = (Button) row.findViewById(R.id.btn2);
            textLink = (TextView) row.findViewById(R.id.tv1);
            progressbar = (TextView) row.findViewById(R.id.progressBar1);
            
                  textLink.setText(downloadservice.
                                      downloads.get(position).getName());
               progressbar.setBottom(downloadservice.
                                     downloads.get(position).getDownloadedSize());
            progressbar.setTop(downloadservice.
                                      downloads.get(position).getTotalSize());
            
            final int p = position;
            final NewDownload r = list.get(p);
            if (r.state=="downloading")
                {
                    progressbar.setText("DownLoading...");
                    pauseButton.setEnabled(true);
                    resumeButton.setEnabled(false);
                }
            else if (r.state=="pause")
                {
                pauseButton.setEnabled(false);
                resumeButton.setEnabled(true);
                progressbar.setText(r.state);
                }
            pauseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                downloadservice.pause(p);
                setListAdapter(listadapter );

                }
            });
                        
            resumeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
            if (getActivity().startService(serviceIntent)==null)
                        
                      getActivity().startService(serviceIntent);
                downloadservice.resume(p);
                setListAdapter(listadapter );
                }
            });
            
            return row;
        }
}

这是下载列表.xml

    <ListView
    android:id="@+id/list" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

这是 list_item.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="File Name" />

<TextView
    android:id="@+id/progressBar1"
    
    android:layout_width="306dp"
    android:layout_height="wrap_content"
     />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >





    <Button
        android:id="@+id/btn1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="34dp"
        android:layout_gravity="center_horizontal"
        android:text="Pause" />




    <Button
        android:id="@+id/btn2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="34dp"
        android:layout_gravity="right"
        android:text="Resume" />

</LinearLayout>

</LinearLayout>

所以任何人都可以告诉我我的错误在哪里?

4

1 回答 1

2

这个错误:

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

意味着您必须使用:

<ListView
    android:id="@android:id/list"
    ...

download.xml.

于 2012-12-10T00:23:28.310 回答