首页
学习
活动
专区
圈层
工具
发布

安卓
EN

Stack Overflow用户
提问于 2013-01-03 05:14:09
回答 1查看 688关注 0票数 0

我想把布局分成两部分,第一部分我只有一个模拟时钟,第二部分我想有一个列表视图。

xml文件;

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/day_plan" />

</LinearLayout>

包含版面的xml;

代码语言:javascript
复制
    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/countryTextView" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:padding="8dp"
   android:textSize="20sp" 
   android:textColor="@android:color/white"
   android:minHeight="?android:attr/listPreferredItemHeight"
   android:gravity="center_vertical"/>

我想在此时钟下显示一个列表,其中包含我在数据库中的事件:

代码语言:javascript
复制
public class Clock extends ListActivity {

    public static final String ROW_ID = "row_id";
     private ListView conListView;
     private CursorAdapter conAdapter;
     public String opis;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.morning_clock);
        conListView=getListView();
        conListView.setOnItemClickListener(viewConListener);

        AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);

        // map each name to a TextView
        String[] from = new String[] { "event" };
        int[] to = new int[] { R.id.countryTextView };
        conAdapter = new SimpleCursorAdapter(Clock.this, R.layout.morning_clock, null, from, to);
        setListAdapter(conAdapter); // set adapter

    }


        @Override
        protected void onResume() 
        {
           super.onResume();  
           new GetEvents().execute((Object[]) null);
         } 


        @Override
        protected void onStop() 
        {
           Cursor cursor = conAdapter.getCursor();

           if (cursor != null) 
              cursor.deactivate();

           conAdapter.changeCursor(null);
           super.onStop();
        }    


        private class GetEvents extends AsyncTask<Object, Object, Cursor> 
        {
           DatabaseConnector dbConnector = new DatabaseConnector(Clock.this);

           @Override
           protected Cursor doInBackground(Object... params)
           {
               Intent a = getIntent();
               String d_m_y = a.getStringExtra("d_m_y");
              dbConnector.open();
              return dbConnector.getdate(d_m_y); 
           } 

           @Override
           protected void onPostExecute(Cursor result)
           {
              conAdapter.changeCursor(result); // set the adapter's Cursor
              dbConnector.close();
           } 
        } 

        @Override
        public boolean onCreateOptionsMenu(Menu menu) 
        {
           super.onCreateOptionsMenu(menu);
           MenuInflater inflater = getMenuInflater();
           inflater.inflate(R.menu.dayplan_menu, menu);
           return true;
        }   

        @Override
        public boolean onOptionsItemSelected(MenuItem item) 
        {
           Intent addEvent = new Intent(Clock.this, AddEvent.class);
           startActivity(addEvent);
           return super.onOptionsItemSelected(item);
        }

        OnItemClickListener viewConListener = new OnItemClickListener() 
        {
           public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
           {         
              Intent viewEvent = new Intent(Clock.this, ViewEvent.class);
              viewEvent.putExtra(ROW_ID, arg3);

              startActivity(viewEvent);
           }
        };    

    }

而我得到了错误:

代码语言:javascript
复制
   01-02 21:57:50.645: E/AndroidRuntime(1047): FATAL EXCEPTION: main

01-02 21:57:50.645: E/AndroidRuntime(1047): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.android.calendar/com.examples.android.calendar.Clock}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.app.ActivityThread.access$600(ActivityThread.java:130)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.os.Handler.dispatchMessage(Handler.java:99)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.os.Looper.loop(Looper.java:137)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at android.app.ActivityThread.main(ActivityThread.java:4745)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at java.lang.reflect.Method.invokeNative(Native Method)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at java.lang.reflect.Method.invoke(Method.java:511)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

01-02 21:57:50.645: E/AndroidRuntime(1047):     at dalvik.system.NativeStart.main(Native Method)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-03 05:15:47

如果要使用listactivity,则所使用的布局必须具有id为@android/list的listview

检查文档here

在这个例子中,你有

代码语言:javascript
复制
<ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#00FF00"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"/>

所以你需要的不是你的文本视图,而是带有android:id="@android:id/list"的ListView

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14129259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档