0

我在列表适配器视图上工作,我在这方面遇到错误,我有两个布局,一个是会话布局,另一个是会话行布局。这是我的代码。

public class SessionListView extends ListActivity{

    private Button prev_btn;
    private Button next_btn;
    private ListView sessionListView;
    private SessionAdapter mAdapter;
    private Context mContext = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.session_layout);
        sessionListView = (ListView) findViewById(R.id.session_list);
        //ListView sessionListView = new ListView(this);

        ArrayList<Session> sessionList = new ArrayList<Session>();

        //initListView(this, sessionListView, "item", 30, android.R.layout.simple_list_item_1);
        prev_btn = (Button) findViewById(R.id.session_prev_btn);
        next_btn = (Button) findViewById(R.id.session_next_btn);


        mAdapter = new SessionAdapter(mContext, R.layout.sessions_row, sessionList);
        sessionListView.setAdapter(mAdapter);
        setupListneres();

    }

    public void initListView(Context context, ListView listView,String prefix, int numItems, int layout ){

        // By using setAdpater method in listview we an add string array in list.
        String[] arr = new String[numItems];
        for(int i = 0; i< arr.length; i++){

            arr[i] = prefix + (i +1);
        }

        listView.setAdapter(new ArrayAdapter<String>(context, layout, arr));
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Context context = view.getContext();

                Intent intent = new Intent(context, SessionDetailsView.class);
                startActivity(intent);

                String msg = "item[" + position + "]= " + parent.getItemIdAtPosition(position);

                Toast.makeText(context, msg, 1000).show();
                System.out.println(msg);

            }
        });

    }

    private void setupListneres() {
        // TODO Auto-generated method stub
        next_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(prev_btn.getVisibility() == View.VISIBLE && next_btn.getVisibility() == View.VISIBLE){ 
                    initListView(getApplicationContext(), sessionListView, "item@third", 30, android.R.layout.simple_list_item_1);
                    next_btn.setVisibility(View.INVISIBLE);
                }else{
                    Toast.makeText(getApplicationContext(), "clicked On button", Toast.LENGTH_SHORT).show();
                    initListView(getApplicationContext(), sessionListView, "item@second", 30, android.R.layout.simple_list_item_1);
                    prev_btn.setVisibility(View.VISIBLE);
                }
            }
        });

        prev_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(prev_btn.getVisibility() == View.VISIBLE && next_btn.getVisibility() == View.VISIBLE){ 
                    initListView(getApplicationContext(), sessionListView, "item", 30, R.layout.my_simple_list_item_1);
                    prev_btn.setVisibility(View.INVISIBLE);
                }else if(prev_btn.getVisibility() == View.VISIBLE && next_btn.getVisibility() != View.VISIBLE){
                    Toast.makeText(getApplicationContext(), "clicked On button", Toast.LENGTH_SHORT).show();
                    initListView(getApplicationContext(), sessionListView, "item@second", 30, android.R.layout.simple_list_item_1);
                    next_btn.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    public class SessionAdapter extends ArrayAdapter<Session>{

        ArrayList<Session> sessionList;

        public SessionAdapter(Context context, int textViewResourceId,
                ArrayList<Session> sesList) {
            super(context, textViewResourceId, sesList);
            sessionList = sesList;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.sessions_row, null);
            }

            // get list of sessions using position
            Session session = this.sessionList.get(position);

            // check if session is not null then assign list value to session_row layout view.
            if(session !=null){

                TextView sessionTitleTV = (TextView)findViewById(R.id.session_title);
                TextView sessionTimeTV = (TextView)findViewById(R.id.session_time);
                TextView sessionVenueTV = (TextView)findViewById(R.id.session_venue);

                String sessionTitle = session.getTitle();
                String sessionTime = "Start" + session.getStartTime() + "End" + session.getEndTime();
                String sessionVenue = "New york";

                sessionTitleTV.setText(sessionTitle);
                sessionTimeTV.setText(sessionTime);
                sessionVenueTV.setText(sessionVenue);
            }
            return v;
        }
    }
}

和我的错误信息,如下。请帮忙。

04-04 05:36:20.566: E/AndroidRuntime(797): FATAL EXCEPTION: main
04-04 05:36:20.566: E/AndroidRuntime(797): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tapon.views/com.tapon.views.SessionListView}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.widget.TabHost.setCurrentTab(TabHost.java:413)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.view.View.performClick(View.java:4204)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.view.View$PerformClick.run(View.java:17355)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.os.Handler.handleCallback(Handler.java:725)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 05:36:20.566: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:36:20.566: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:511)
04-04 05:36:20.566: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 05:36:20.566: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 05:36:20.566: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)
04-04 05:36:20.566: E/AndroidRuntime(797): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-04 05:36:20.566: E/AndroidRuntime(797):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.Activity.setContentView(Activity.java:1881)
04-04 05:36:20.566: E/AndroidRuntime(797):  at com.example.views.SessionListView.onCreate(SessionListView.java:35)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.Activity.performCreate(Activity.java:5104)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 05:36:20.566: E/AndroidRuntime(797):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 05:36:20.566: E/AndroidRuntime(797):  ... 18 more
4

2 回答 2

4

如果你在你的布局中使用 a应该ListActivity是@android :id/list,就像在这个例子中一样:idListView

 <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"/>
于 2013-04-04T05:56:16.167 回答
2

在 ListView 中的 session_layout.xml 中,将 id 更改为

android:id="@android:id/list" 
于 2013-04-04T05:56:00.070 回答