0

我想首先将布局划分为两个部分,我只有一个模拟时钟,第二个我想要一个列表视图。

xml文件;

<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;

    <?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"/>

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

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);
           }
        };    

    }

我得到错误:

   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)
4

1 回答 1

1

如果你想使用listactivity,你使用的布局必须有一个id为@android/list的listview

在此处查看文档

在您的示例中

<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"/>

所以你真的需要一个ListView而 不是你的textviewandroid:id="@android:id/list"

于 2013-01-02T21:15:47.287 回答