我正在使用Threeten时区在android中获取日期和时间。但是在AndroidThreeTen.init((Application)getContext());处出错错误为“ java.lang.ClassCastException:moneytracker.karthik.example.moneytracker.Display.ViewCash无法转换为android.app.Application”。
这是我的代码:
package moneytracker.karthik.example.moneytracker.Display;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jakewharton.threetenabp.AndroidThreeTen;
import org.threeten.bp.*;
import java.util.ArrayList;
import java.util.List;
import moneytracker.karthik.example.moneytracker.Adapter.TimeAdapter;
import moneytracker.karthik.example.moneytracker.R;
public class WeekCash extends Fragment {
private TimeAdapter Adapter;
private RecyclerView recyclerView;
private String[] WeekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
AndroidThreeTen.init((Application) getContext());
View rootView = inflater.inflate(R.layout.week_cash, container, false);
recyclerView = rootView.findViewById(R.id.rc_totalDisplayWk);
setRecycleView();
return rootView;
}
public void setRecycleView() {
Context context = getContext();
Adapter = new TimeAdapter(context, WeekDays, getWeekDays());
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(Adapter);
}
public List<LocalDate> getWeekDays() {
// AndroidThreeTen.init((Application) getContext());
ZoneId z = ZoneId.of("Pacific/Auckland"); // Or ZoneId.of( "Africa/Tunis" )
LocalDate today = LocalDate.now( z ) ;
LocalDate localDate = today.with( org.threeten.bp.temporal.TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) ) ;
List< LocalDate > dates = new ArrayList<>( 7 ) ;
for( int i = 0 ; i < 7 ; i ++ ) {
localDate = localDate.plusDays( i ) ;
dates.add( localDate ) ;
}
return dates;
}
}
即使我尝试了AndroidThreeTen.init((Application)getApplicationContext());。它表明AndroidThreeTen没有该方法。请帮助我
答案 0 :(得分:0)
从更改代码行:
AndroidThreeTen.init((Application) getContext());
收件人:
AndroidThreeTen.init((Application) getContext().getApplicationContext());