2013-02-26 3 views
5

私はダイアログでタイムピッカーを作成しようとしていると私はフラグメントクラスのことわざにエラーが表示されます。アンドロイド:時刻ピッカーダイアログ

メソッドis24HourFormat(活動)タイプ にDateFormatのために定義されていません次の行にあります。

ラインで

DateFormat.is24HourFormat(getActivity())); 

私はすべてのエラーを期待していなかったので、これはストレートAndroidデベロッパーのウェブサイトオフの例です。私は以下のコードを示しました。

誰かがこれに私を助けることができたら、それは最も高く評価されます。

おかげ

コード

package com.app.timer2; 

import android.app.Activity; 
import android.app.DialogFragment; 
import android.os.Bundle; 
import android.view.View; 

import com.app.timer.R; 





public class SecondActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second);} 

    public void showTimePickerDialog(View v) { 
     DialogFragment newFragment = new TimePickerFragment(); 
     newFragment.show(getFragmentManager(), "timePicker"); 
    } 


} 

断片

package com.app.timer2; 

import java.text.DateFormat; 
import java.util.Calendar; 

import android.app.Dialog; 
import android.app.DialogFragment; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.widget.TimePicker; 



public class TimePickerFragment extends DialogFragment 
implements TimePickerDialog.OnTimeSetListener { 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
// Use the current time as the default values for the picker 
final Calendar c = Calendar.getInstance(); 
int hour = c.get(Calendar.HOUR_OF_DAY); 
int minute = c.get(Calendar.MINUTE); 

// Create a new instance of TimePickerDialog and return it 
return new TimePickerDialog(getActivity(), this, hour, minute, 
DateFormat.is24HourFormat(getActivity())); 
} 

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
// Do something with the time chosen by the user 
} 
} 

XML

<LinearLayout 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" 
    tools:context=".MainActivity" 
    android:orientation="vertical" > 

     <Button 
     android:id="@+id/start_time_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:padding="10dp" 
     android:text="@string/start_time_button" 
     android:onClick="showTimePickerDialog" /> 

</LinearLayout> 

答えて

13

java.text.DateFormatをインポートしたので、代わりにandroid.text.format.DateFormatをインポートする必要があります。間違ったクラスを使用しています。

3

これは、間違ったDateFormatを使用しているためです。

あなたはしている:

import java.text.DateFormat; 

したいときは:要するに

import android.text.format.DateFormat 

を、これら2つのオブジェクトが同じ名前を持っていますが、それらは同じオブジェクトではありません。だから、Contextを扱う方法を知っている唯一の人であるので、あなたはDateFormat.is24HourFormat(getActivity()));と呼んでいる方法で提供されたアンドロイドを使い分ける必要があります。