在我的活动中,我想在顶部显示一些文本视图,然后在它下面充气一个listview。我尝试过各种方法没有成功,比如addview(),但无法让它工作。我膨胀的观点成为主要布局。Android:如何将充气布局和非充气布局结合起来?
任何帮助将不胜感激。基于泰德霍普溶液 logcat的错误
2月10日至20日:38:38.322:d/AndroidRuntime(1253):关闭VM 2月10日至20日:38:38.322:W/dalvikvm(1253):主题ID = 3:螺纹带未捕获的异常退出(组= 0x4001b188) 2月10日至20日:38:38.322:E/AndroidRuntime(1253):未捕获的处理程序:线程退出主要由于未捕获的异常 2月10日至20日:38:38.342:电子/ AndroidRuntime(1253):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.mycoursetimetable/com.example.mycoursetimetable.MyCourses}:java.lang.IllegalStateException:无法将标题视图添加到列表中 - setAdapter已经被称为。 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 二月10日至20日:38:38.342:E/AndroidRuntime(1253):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119) -10- 20 02:38:38.342:E/AndroidRuntime(1253):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863) 10-20 02:38:38.342:E/AndroidRuntime(1253):at android .os.Handler.dispatchMessage(Handler.java:99) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在android.os.Looper.loop(Looper.java:123) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在android.app.ActivityThread.main(ActivityThread.java:4363) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在java.lang.reflect.Method.invokeNative(本机方法) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在java的.lang.reflect.Method.invoke(Method.java:521) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :860) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 2月10日至20日:38:38.342:电子/ AndroidRuntime(1253):在dalvik.system.NativeStart.main(本机方法) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):java.lang.IllegalStateException:致无法添加报头以列出 - setAdapter已被调用。 二月10日至20日:38:38.342:E/AndroidRuntime(1253):在android.widget.ListView.addHeaderView(ListView.java:256) 二月10日至20日: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) 2月10日至20日:38:38.342:E/AndroidRuntime(1253):在机器人。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 2月10日至二十○日:38:38.342:E/AndroidRuntime(1253):... 11多个 2月10日至二十○日:38:38.382:I/dalvikvm(1253) :threadid = 7:对信号做出反应3 10-20 02:38:38.382:E/dalvikvm(1253):Unabl e打开堆栈跟踪文件'/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>
listcourses
<?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;
}
}
在的onCreate的开始()方法,你不调用的setContentView()method..so难怪你找不到你的主要的LinearLayout –
目前尚不清楚你想要做什么。你没有使用你在'onCreate'中分配的'main'变量。另外,布局'activity_my_courses'不能像'ListActivity'的布局那样使用。它需要有一个'ListView'属性为'android:id =“@ android:id/list”'。 –