2

在我的活动中,我想在顶部显示一些文本视图,然后在它下面膨胀一个列表视图。我尝试了各种方法但均未成功,例如 addview() 但无法使其正常工作。我的膨胀视图成为主要布局。

任何帮助将非常感激。基于 Ted Hopp 解决方案的 LogCat 错误

10-20 02:38:38.322: D/AndroidRuntime(1253): 关闭 VM 10-20 02:38:38.322: W/dalvikvm(1253): threadid=3: 线程退出未捕获异常 (group=0x4001b188) 10 -20 02:38:38.322:E/AndroidRuntime(1253):未捕获的处理程序:线程主因未捕获的异常而退出 10-20 02:38:38.342:E/AndroidRuntime(1253):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.MyCourses}:java.lang.IllegalStateException:无法将标题视图添加到列表 - 已调用 setAdapter。10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android. app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 10-20 02:38:38.342: E/AndroidRuntime(1253): E/AndroidRuntime(1253): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 10-20 02:38:38.342: E/AndroidRuntime(1253): 在 dalvik.system.NativeStart.main (本机方法)10-20 02:38:38.342:E/AndroidRuntime(1253):原因:java.lang.IllegalStateException:无法将标头视图添加到列表 - 已调用 setAdapter。10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.widget.ListView.addHeaderView(ListView.java:256) 10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android. widget.ListView.addHeaderView(ListView.java:279) 10-20 02:38:38.342: E/AndroidRuntime(1253): at com.example.mycoursetimetable.MyCourses.onCreate(MyCourses.java:30) 10-20 02: 38:38.342: E/AndroidRuntime(1253): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 10-20 02:38:38.342: E/AndroidRuntime(1253): ... 11 更多 10-20 02:38:38.382: I/dalvikvm(1253 ):threadid = 7:对信号3 10-20 02:38:38.382做出反应:E/dalvikvm(1253):无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝10-20 02: 38:47.463:I/Process(1253):发送信号。PID:1253 SIG:9

activity_my_courses

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/main" >

    <Button
        android:id="@+id/labelAddCourseButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addCourseButton"
        android:padding="10dp"
        android:text="@string/CourseName" />

</LinearLayout>

列表课程

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



    <TextView
        android:id="@+id/labelModuleCode"
        android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:layout_marginLeft="10dp"
        android:text="@string/CourseName"
        android:textSize="10dp" />

     <TextView
        android:id="@+id/labelCourseType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CourseType"
        android:layout_marginLeft="10dp"
        android:textSize="10dp" />
</LinearLayout>

代码

public class MyCourses extends ListActivity {

    static final String TEST = "com.example.mycoursetimetable.TEST";
    String [] MODULE;

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);

        LinearLayout main = (LinearLayout)this.findViewById(R.id.main);
        MODULE = getResources().getStringArray(R.array.module);
        setListAdapter(new ListArrayAdapter(this,MODULE));
        }



        public void addCourseButton (View addCourseButton) 
        {
         Intent intent = new Intent(this,AddCourse.class);
         startActivity(intent);
        }

        protected void onListItemClick(ListView l, View v, int position, long id)
        {
          super.onListItemClick(l, v, position, id);


        try {
            Class test = Class.forName("com.example.MyCourseTimeTable.AddCourse");
            Intent intent = new Intent(MyCourses.this, test);

            TextView textView = (TextView) v.findViewById(R.id.labelModuleCode);
            String module = textView.getText().toString();


            intent.putExtra(TEST,module);
            startActivity(intent);
            }   
                catch (ClassNotFoundException e)
                {
                e.printStackTrace();
                }

        }

}

class ListArrayAdapter extends ArrayAdapter<String> 
{
    //  You only need one copy of LayoutInflater
    private final LayoutInflater inflater;

    //create the ArrayAdpater
    public ListArrayAdapter(Context context, String[] test) 
    {
        super(context, R.layout.activity_my_courses, test);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        if(convertView == null) {


            // Inflate the layout that you want for each row


            convertView = inflater.inflate(R.layout.listcourses, parent, false);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.labelModuleCode);
            holder.type = (TextView) convertView.findViewById(R.id.labelCourseType);

            // Add a link for your button as well and define its OnClickListener here to, eventually...

            convertView.setTag(holder);
        }
        else 
            holder = (ViewHolder) convertView.getTag();

        String module = getItem(position);
        //set the text to the string values based on position
        holder.code.setText(module);

        //return the layout
        return convertView;
    }

    class ViewHolder {
        TextView code;
        TextView type;
    }
}
4

2 回答 2

3

与其尝试定义自己的活动布局,不如使用如下内容:

public class MyCourses extends ListActivity {

    static final String TEST = "com.example.mycoursetimetable.TEST";
    String [] MODULE;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        MODULE = getResources().getStringArray(R.array.module);
        getListView().addHeaderView(getHeader()); // must come before next line
        setListAdapter(new ListArrayAdapter(this,MODULE));
    }

    private View getHeader() {
        LayoutInflater inflater =
            (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.listcourses, null);
    }
    . . .
}

这将使视图在layout/listcourses.xml列表的标题中定义。

于 2012-10-31T15:06:45.400 回答
0

我想你需要<ListView/>在 xml 文件中的 textviews 下面有一个 android id 应该是默认android:id\list的,其次你需要setContentView(YOUR LAYOUT FILE)在你的onCreate()

于 2012-10-31T15:04:34.457 回答