如何将ListView与SherlockActivity一起使用

时间:2012-11-16 18:51:07

标签: android adapter actionbarsherlock

使用listview和SherlockActivity时遇到一些问题。 我的问题是我在这一行上得到了空白:

ListView    lv = (ListView) findViewById(R.id.mainListView ); 

我的xml看起来像这样:

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

    <ListView android:layout_width="fill_parent"   
      android:layout_height="fill_parent"   
      android:id="@+id/mainListView">  

    <ImageView
        android:id="@+id/iconremove"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:scaleType="centerCrop"
         android:src="@drawable/removeicon"
         android:visibility="gone"
          />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="10dip"
        android:layout_weight="1"
        android:textSize="20dip" />
      </ListView>    
</LinearLayout>

我的代码看起来像这样:

public class ListRss extends SherlockActivity  {
    ActionBar actionbar; 
    @SuppressWarnings("unchecked")
    public ArrayList<rssLinkAndTitle> ListRssLinkAndTitle=new ArrayList();
    //public ArrayList listValue=new ArrayList();
    RssArrays rssparser=new RssArrays();
    Context con;
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);

            actionbar=getSupportActionBar();
            con=getApplicationContext();
            ListRssAdpter listAdapter;
        //  ListView lv = getListView();

            ListView    lv = (ListView) findViewById(R.id.mainListView );

感谢您的帮助!

更新:

添加了这个

    super.onCreate(savedInstanceState);
           setContentView(R.layout.titlesrss);

错误出现在这一行上:

11-17 07:57:46.341: E/AndroidRuntime(481): FATAL EXCEPTION: main
11-17 07:57:46.341: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rss/com.example.rss.ListRss}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.os.Looper.loop(Looper.java:123)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-17 07:57:46.341: E/AndroidRuntime(481):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 07:57:46.341: E/AndroidRuntime(481):  at java.lang.reflect.Method.invoke(Method.java:507)
11-17 07:57:46.341: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-17 07:57:46.341: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-17 07:57:46.341: E/AndroidRuntime(481):  at dalvik.system.NativeStart.main(Native Method)
11-17 07:57:46.341: E/AndroidRuntime(481): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.widget.AdapterView.addView(AdapterView.java:461)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:627)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-17 07:57:46.341: E/AndroidRuntime(481):  at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:853)
11-17 07:57:46.341: E/AndroidRuntime(481):  at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:229)
11-17 07:57:46.341: E/AndroidRuntime(481):  at com.example.rss.ListRss.onCreate(ListRss.java:54)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 07:57:46.341: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-17 07:57:46.341: E/AndroidRuntime(481):  ... 11 more

1 个答案:

答案 0 :(得分:4)

我想你忘了设置你的布局了。所以,在

之后
            super.onCreate(savedInstanceState);

添加此

            setContentView(R.layout.nameOfXmlFileYouPostedWeDontKnowTheNameOf);

其中nameOfXmlFileYouPostedWeDontKnowTheNameOf是xml文件的名称