안드로이드 타겟버전은 정하지않고 최소버전이 12인 상태에서 Theme.Holo.Light 테마를 쓰는 상태일경우

DatePickerDialog, DatePickerDialog가 앱이 죽는 현상 발생


현재 매니페스트 상태

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdatepicker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="12"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


해결방법

날짜

private void showCalendarDialog() {
Context context = null;
DatePickerDialog dialog = null;
Calendar calendar = Calendar.getInstance();

// 안드로이드 7.0버전부터 Theme.Holo.Light를 지원하지 않음
if (Build.VERSION.SDK_INT >= 24) {
context = new ContextThemeWrapper(mContext, android.R.style.Theme_DeviceDefault_Light_Dialog);
}else{
context = mContext;
}

dialog = new DatePickerDialog(context, mDateSetListener, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}


private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub

}

};


시간

private void showTimeDialog() {

Context context = null;
TimePickerDialog dialog = null;
Calendar calendar = Calendar.getInstance();

// 안드로이드 7.0버전부터 Theme.Holo.Light를 지원하지 않음
if (Build.VERSION.SDK_INT >= 24) {
context = new ContextThemeWrapper(mContext, android.R.style.Theme_DeviceDefault_Light_Dialog);
}else{
context = mContext;
}

dialog = new TimePickerDialog(context, mTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY), 0, true);
dialog.show();
}

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub

}

};



어떤 테마스타일 이쁜지 확인하고 싶을경우

MainActivity.class


import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
private final static boolean isShowDatePickerDialog = true; // DatePickerDialog 노출여부

private Context mContext = null;
private LinearLayout mLinearLayout = null;
private ListView mListView = null;
private int[] mStyleIdArr = {
android.R.style.Theme,
android.R.style.Theme_Black,
android.R.style.Theme_Black_NoTitleBar,
android.R.style.Theme_Black_NoTitleBar_Fullscreen,
android.R.style.Theme_DeviceDefault,
android.R.style.Theme_DeviceDefault_Dialog,
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
android.R.style.Theme_DeviceDefault_Dialog_NoActionBar,
android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth,
android.R.style.Theme_DeviceDefault_DialogWhenLarge,
android.R.style.Theme_DeviceDefault_DialogWhenLarge_NoActionBar,
android.R.style.Theme_DeviceDefault_InputMethod,
android.R.style.Theme_DeviceDefault_Light,
android.R.style.Theme_DeviceDefault_Light_DarkActionBar,
android.R.style.Theme_DeviceDefault_Light_Dialog,
android.R.style.Theme_DeviceDefault_Light_Dialog_MinWidth,
android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar,
android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth,
android.R.style.Theme_DeviceDefault_Light_DialogWhenLarge,
android.R.style.Theme_DeviceDefault_Light_DialogWhenLarge_NoActionBar,
android.R.style.Theme_DeviceDefault_Light_NoActionBar,
android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen,
android.R.style.Theme_DeviceDefault_Light_Panel,
android.R.style.Theme_DeviceDefault_NoActionBar,
android.R.style.Theme_DeviceDefault_NoActionBar_Fullscreen,
android.R.style.Theme_DeviceDefault_Panel,
android.R.style.Theme_DeviceDefault_Wallpaper,
android.R.style.Theme_DeviceDefault_Wallpaper_NoTitleBar,
android.R.style.Theme_Dialog, android.R.style.Theme_Holo,
android.R.style.Theme_Holo_Dialog,
android.R.style.Theme_Holo_Dialog_MinWidth,
android.R.style.Theme_Holo_Dialog_NoActionBar,
android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth,
android.R.style.Theme_Holo_DialogWhenLarge,
android.R.style.Theme_Holo_DialogWhenLarge_NoActionBar,
android.R.style.Theme_Holo_InputMethod,
android.R.style.Theme_Holo_Light,
android.R.style.Theme_Holo_Light_DarkActionBar,
android.R.style.Theme_Holo_Light_Dialog,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth,
android.R.style.Theme_Holo_Light_DialogWhenLarge,
android.R.style.Theme_Holo_Light_DialogWhenLarge_NoActionBar,
android.R.style.Theme_Holo_Light_NoActionBar,
android.R.style.Theme_Holo_Light_NoActionBar_Fullscreen,
android.R.style.Theme_Holo_Light_Panel,
android.R.style.Theme_Holo_NoActionBar,
android.R.style.Theme_Holo_NoActionBar_Fullscreen,
android.R.style.Theme_Holo_Panel,
android.R.style.Theme_Holo_Wallpaper,
android.R.style.Theme_Holo_Wallpaper_NoTitleBar,
android.R.style.Theme_InputMethod, android.R.style.Theme_Light,
android.R.style.Theme_Light_NoTitleBar,
android.R.style.Theme_Light_NoTitleBar_Fullscreen,
android.R.style.Theme_Light_Panel,
android.R.style.Theme_Light_WallpaperSettings,
android.R.style.Theme_NoDisplay, android.R.style.Theme_NoTitleBar,
android.R.style.Theme_NoTitleBar_Fullscreen,
android.R.style.Theme_NoTitleBar_OverlayActionModes,
android.R.style.Theme_Panel, android.R.style.Theme_Translucent,
android.R.style.Theme_Translucent_NoTitleBar,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen,
android.R.style.Theme_Wallpaper,
android.R.style.Theme_Wallpaper_NoTitleBar,
android.R.style.Theme_Wallpaper_NoTitleBar_Fullscreen,
android.R.style.Theme_WallpaperSettings,
android.R.style.Theme_WithActionBar };

private String[] mStyleNameArr = { "Theme", "Theme_Black",
"Theme_Black_NoTitleBar", "Theme_Black_NoTitleBar_Fullscreen",
"Theme_DeviceDefault", "Theme_DeviceDefault_Dialog",
"Theme_DeviceDefault_Dialog_MinWidth",
"Theme_DeviceDefault_Dialog_NoActionBar",
"Theme_DeviceDefault_Dialog_NoActionBar_MinWidth",
"Theme_DeviceDefault_DialogWhenLarge",
"Theme_DeviceDefault_DialogWhenLarge_NoActionBar",
"Theme_DeviceDefault_InputMethod", "Theme_DeviceDefault_Light",
"Theme_DeviceDefault_Light_DarkActionBar",
"Theme_DeviceDefault_Light_Dialog",
"Theme_DeviceDefault_Light_Dialog_MinWidth",
"Theme_DeviceDefault_Light_Dialog_NoActionBar",
"Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth",
"Theme_DeviceDefault_Light_DialogWhenLarge",
"Theme_DeviceDefault_Light_DialogWhenLarge_NoActionBar",
"Theme_DeviceDefault_Light_NoActionBar",
"Theme_DeviceDefault_Light_NoActionBar_Fullscreen",
"Theme_DeviceDefault_Light_Panel",
"Theme_DeviceDefault_NoActionBar",
"Theme_DeviceDefault_NoActionBar_Fullscreen",
"Theme_DeviceDefault_Panel", "Theme_DeviceDefault_Wallpaper",
"Theme_DeviceDefault_Wallpaper_NoTitleBar", "Theme_Dialog",
"Theme_Holo", "Theme_Holo_Dialog", "Theme_Holo_Dialog_MinWidth",
"Theme_Holo_Dialog_NoActionBar",
"Theme_Holo_Dialog_NoActionBar_MinWidth",
"Theme_Holo_DialogWhenLarge",
"Theme_Holo_DialogWhenLarge_NoActionBar", "Theme_Holo_InputMethod",
"Theme_Holo_Light", "Theme_Holo_Light_DarkActionBar",
"Theme_Holo_Light_Dialog", "Theme_Holo_Light_Dialog_MinWidth",
"Theme_Holo_Light_Dialog_NoActionBar",
"Theme_Holo_Light_Dialog_NoActionBar_MinWidth",
"Theme_Holo_Light_DialogWhenLarge",
"Theme_Holo_Light_DialogWhenLarge_NoActionBar",
"Theme_Holo_Light_NoActionBar",
"Theme_Holo_Light_NoActionBar_Fullscreen",
"Theme_Holo_Light_Panel", "Theme_Holo_NoActionBar",
"Theme_Holo_NoActionBar_Fullscreen", "Theme_Holo_Panel",
"Theme_Holo_Wallpaper", "Theme_Holo_Wallpaper_NoTitleBar",
"Theme_InputMethod", "Theme_Light", "Theme_Light_NoTitleBar",
"Theme_Light_NoTitleBar_Fullscreen", "Theme_Light_Panel",
"Theme_Light_WallpaperSettings", "Theme_NoDisplay",
"Theme_NoTitleBar", "Theme_NoTitleBar_Fullscreen",
"Theme_NoTitleBar_OverlayActionModes", "Theme_Panel",
"Theme_Translucent", "Theme_Translucent_NoTitleBar",
"Theme_Translucent_NoTitleBar_Fullscreen", "Theme_Wallpaper",
"Theme_Wallpaper_NoTitleBar",
"Theme_Wallpaper_NoTitleBar_Fullscreen", "Theme_WallpaperSettings",
"Theme_WithActionBar" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
mListView = (ListView) findViewById(R.id.test_list);
for (int i = 0; i < mStyleIdArr.length; i++) {
Button btnButton = new Button(this);
btnButton.setText(mStyleNameArr[i]);
}
ArrayAdapter<String> testAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStyleNameArr);
mListView.setAdapter(testAdapter);
mListView.setOnItemClickListener(this);

}

//날짜 다이얼로그
private void showCalendarDialog(int style) {
Calendar calendar = Calendar.getInstance();

DatePickerDialog dialog = null;

dialog = new DatePickerDialog(mContext, style, mDateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));

dialog.show();

}

//시간 다이얼로그
private void showTimeDialog(int style) {
Calendar calendar = Calendar.getInstance();
TimePickerDialog dialog = null;
dialog = new TimePickerDialog(mContext, style, mTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY), 0, true);

dialog.show();

}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub

}

};

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub

}

};

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(this, "position" + position, Toast.LENGTH_SHORT).show();
if(isShowDatePickerDialog) {
showCalendarDialog(mStyleIdArr[position]);
}else{
showTimeDialog(mStyleIdArr[position]);
}
}


}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<ListView
android:id="@+id/test_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

</RelativeLayout>


728x90
반응형

+ Recent posts