「Datetime」というクラスがあり、インテントを使用して Datetime のアクティビティを MainActivity に呼び出したいと考えています。
これは私がしたことです
public void onClick(View view) {
Intent i = new Intent(this, Datetime.class);
startActivity(i);
}
また、マニフェストも更新しました。
<activity
android:name=".Datetime" >
</activity>
しかし、うまくいきません。コードの何が問題になっていますか?
念のため、参考にさせていただきます。以下は私の日時クラスです:
public class Datetime extends Activity {
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 2;
private Button pickDate;
private int year, month, day;
private Button pickTime;
private int hours, min;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_layout);
pickDate = (Button)findViewById(R.id.date_button);
pickDate.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
updateDate();
pickTime = (Button)findViewById(R.id.time_button);
pickTime.setOnClickListener( new OnClickListener () {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
hours = cal.get(Calendar.HOUR);
min = cal.get(Calendar.MINUTE);
updateTime();
}
private void updateTime() {
pickTime.setText(new StringBuilder().append(hours).append(':')
.append(min));
}
private void updateDate() {
pickDate.setText(new StringBuilder().append(day).append('-')
.append(month + 1).append('-').append(year));
}
private DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
year = yr;
month = monthOfYear;
day = dayOfMonth;
updateDate();
}
};
private TimePickerDialog.OnTimeSetListener timeListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hours = hourOfDay;
min = minute;
updateTime();
}
};
protected Dialog onCreateDialog(int id){
switch(id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateListener, year, month, day);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeListener, hours, min, false);
}
return null;
}
}
ログキャット:
07-03 16:36:37.394: E/Dynamiclayout(1282): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
07-03 16:36:37.394: E/Dynamiclayout(1282): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-03 16:36:42.189: D/GestureDetector(1282): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
要求に応じて、私の MainActivity クラス:
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
// Initialize a TabSpec for each tab and add it to the TabHost
intent = new Intent().setClass(this, ViewTab.class);
spec = tabHost.newTabSpec("View").setIndicator("View",
res.getDrawable(R.drawable.ic_tab_view))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CreateTab.class);
// Do the same for the other tabs
spec = tabHost.newTabSpec("Create").setIndicator("Create",
res.getDrawable(R.drawable.ic_tab_create))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_about:
startActivity(new Intent(this,About.class));
return true;
}
return false;
}
public void onClick(View view) {
Intent i = new Intent(this, Datetime.class);
startActivity(i);
}
}