0

我正在使用 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 没有该方法。请帮我

4

1 回答 1

0

更改您的代码行:

AndroidThreeTen.init((Application) getContext());

至:

AndroidThreeTen.init((Application) getContext().getApplicationContext());
于 2018-10-15T16:43:37.130 回答