我试图在按钮和文本视图下放置一个列表。 什么是最好的选择。
我现在正尝试在线性布局下使用片段。
但是我的应用程序在onCreate中崩溃了(我猜是这样),
在日志中显示:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javacodegeeks.android.datepickerexample/com.javacodegeeks.android.datepickerexample.MainActivity}: android.view.InflateException: Binary XML file line #52: Error inflating class fragment
这是代码:
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ch_label" />
<Button
android:id="@+id/button_start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ch_start_time_label" />
<Button
android:id="@+id/button_end_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ch_end_time_label" />
<TextView
android:id="@+id/date_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cur_date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/start_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/end_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment android:name="com.javadocsgeeks.android.datepickerexample"
android:id="@+id/c_sms_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout >
列表中每行的row_view.xml(ListFragment)
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="?android:attr/activatedBackgroundIndicator"
android:text="CheckedTextView" />
MainActivity.java
public class MainActivity extends Activity {
private TextView text_date;
private DatePicker date_picker;
private TimePicker start_time_picker;
private TimePicker end_time_picker;
private Button date_button;
private Button start_time_button;
private Button end_time_button;
private int year;
private int month;
private int day;
private int start_hour;
private int start_minute;
private int end_hour;
private int end_minute;
static final int DATE_DIALOG_ID = 100;
static final int START_TIME_DIALOG_ID = 101;
static final int END_TIME_DIALOG_ID = 102;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDate();
addButtonListener();
}
// display current date both on the text view and the Date Picker
public void setCurrentDate() {
text_date = (TextView) findViewById(R.id.text_date);
date_picker = (DatePicker) findViewById(R.id.date_picker);
start_time_picker = (TimePicker) findViewById(R.id.start_time_picker);
end_time_picker = (TimePicker) findViewById(R.id.end_time_picker);
final Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
start_hour = 7;
start_minute = 0;
end_hour += 8;
end_minute = 0;
String date_string = new StringBuilder()
.append(day).append("-")
.append(month + 1).append("-")
.append(year).append(" ").toString();
String start_time_string = new StringBuilder()
.append(" START time ")
.append(pad(start_hour)).append(":")
.append(pad(start_minute)).append(" ").toString();
String end_time_string = new StringBuilder()
.append(" END time ")
.append(pad(end_hour)).append(":")
.append(pad(end_minute)).append(" ").toString();
// set current date into textview
text_date.setText(new StringBuilder()
// Month is 0 based, so you have to add 1
.append(date_string)
.append(start_time_string)
.append(end_time_string)
);
// set current date into Date Picker
date_picker.init(year, month, day, null);
date_picker.setVisibility(DatePicker.GONE);
date_button = (Button) findViewById(R.id.button_date);
date_button.setText(date_string);
start_time_button = (Button) findViewById(R.id.button_start_time);
end_time_button = (Button) findViewById(R.id.button_end_time);
initTimePickerAndButton(start_time_picker, start_hour, start_minute, start_time_button, start_time_string);
initTimePickerAndButton(end_time_picker, end_hour, end_minute, end_time_button, end_time_string);
}
public void addButtonListener() {
// Date Picker
date_button = (Button) findViewById(R.id.button_date);
date_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// Start Time Picker
start_time_button = (Button) findViewById(R.id.button_start_time);
start_time_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(START_TIME_DIALOG_ID);
}
});
// End Time Picker
end_time_button = (Button) findViewById(R.id.button_end_time);
end_time_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(END_TIME_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,day);
case START_TIME_DIALOG_ID:
// set date picker as current date
return new TimePickerDialog(this, startTimePickerListener, start_hour, start_minute, false);
case END_TIME_DIALOG_ID:
// set date picker as current date
return new TimePickerDialog(this, endTimePickerListener, end_hour, end_minute, false);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into Text View
text_date.setText(new StringBuilder()
// Month is 0 based, so you have to add 1
.append(day).append("-")
.append(month + 1).append("-")
.append(year).append(" ")
.append(year).append(" at time ")
.append(pad(start_hour)).append(":")
.append(pad(start_minute)).append(" ")
);
// set selected date into Date Picker
date_picker.init(year, month, day, null);
}
};
private TimePickerDialog.OnTimeSetListener startTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
start_hour = selectedHour;
start_minute = selectedMinute;
String date_string = new StringBuilder()
.append(day).append("-")
.append(month + 1).append("-")
.append(year).append(" ").toString();
String start_time_string = new StringBuilder()
.append(" start time ")
.append(pad(start_hour)).append(":")
.append(pad(start_minute)).append(" ").toString();
String end_time_string = new StringBuilder()
.append(" start time ")
.append(pad(end_hour)).append(":")
.append(pad(end_minute)).append(" ").toString();
// set current date into textview
text_date.setText(new StringBuilder()
// Month is 0 based, so you have to add 1
.append(date_string)
.append(start_time_string)
.append(end_time_string)
);
initTimePickerAndButton(start_time_picker, start_hour, start_minute, start_time_button, start_time_string);
}
};
private TimePickerDialog.OnTimeSetListener endTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
end_hour = selectedHour;
end_minute = selectedMinute;
String date_string = new StringBuilder()
.append(day).append("-")
.append(month + 1).append("-")
.append(year).append(" ").toString();
String start_time_string = new StringBuilder()
.append(" start time ")
.append(pad(start_hour)).append(":")
.append(pad(start_minute)).append(" ").toString();
String end_time_string = new StringBuilder()
.append(" start time ")
.append(pad(end_hour)).append(":")
.append(pad(end_minute)).append(" ").toString();
// set current date into textview
text_date.setText(new StringBuilder()
// Month is 0 based, so you have to add 1
.append(date_string)
.append(start_time_string)
.append(end_time_string)
);
initTimePickerAndButton(end_time_picker, end_hour, end_minute, end_time_button, end_time_string);
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
private void initTimePickerAndButton(TimePicker time_picker, int hour, int minute, Button time_button, String time_string) {
time_picker.setVisibility(TimePicker.GONE);
time_picker.setIs24HourView(Boolean.TRUE);
time_picker.setCurrentHour(hour);
time_picker.setCurrentMinute(minute);
time_button.setText(time_string);
}
}
c_sms_list.java(这是我希望它列出的片段)
package com.javacodegeeks.android.datepickerexample;
import android.app.ListActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment ;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
import android.widget.ListView;
public class c_sms_list extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] values = new String[] { "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "w", "x", "y", "z" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_multiple_choice, values);
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
答案 0 :(得分:3)
更新main.xml:
<fragment android:name="com.javadocsgeeks.android.datepickerexample"
android:id="@+id/c_sms_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
=&GT;
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/c_sms_list">
</LinearLayout>
更新您的主要活动,将以下代码添加到onCreate方法的底部:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction trans = fragmentManager.beginTransaction();
c_sms_list yourFrag = new c_sms_list();
trans.add(R.id.c_sms_list, yourFrag);
trans.commit();
答案 1 :(得分:0)
为什么要在onActivityCreated函数中调用super.onCreate()
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//... your code
}
到:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); // change from calling onCreate to onActivityCreated
//... your code
}
并且在name属性中的fragment标签的主xml文件中,您错过了片段类名称 所以改变这个
<fragment android:name="com.javadocsgeeks.android.datepickerexample"
other attributes = ""
/>
到
<fragment android:name="com.javadocsgeeks.android.datepickerexample.c_sms_list"
other attributes = ""
/>
答案 2 :(得分:0)
你错过了xml中片段的名称:
<fragment android:name="com.javadocsgeeks.android.datepickerexample.c_sms_list"
here您可以找到文档