崩溃[java.lang.NoClassDefFoundError:无法解决以下问题:Ljava / time / format / DateTimeFormatter]较旧的android版本,尤其是android版本7.0和7.1。基本上,我是在改造2的帮助下并使用LocalTime
的对象来调用api。下面是Java代码和日志
MainActivity.Java
public class PrayerActivity extends AppCompatActivity {
Dialog dialog;
TextView fajrTv,dhuhrTv,asrTv,maghribTv,ishaTv,dateTv,locationTv;
Button btnMethod,btnSearch;
AutoCompleteTextView search;
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prayer);
btnMethod = findViewById(R.id.btnMethod);
btnSearch=findViewById(R.id.btnSearch);
fajrTv=findViewById(R.id.fajrTv);
dhuhrTv=findViewById(R.id.dhuhrTv);
asrTv=findViewById(R.id.asrTv);
maghribTv=findViewById(R.id.maghribTv);
ishaTv=findViewById(R.id.ishaTv);
dateTv=findViewById(R.id.dateTv);
locationTv=findViewById(R.id.locationTv);
search=findViewById(R.id.search);
linearLayout=findViewById(R.id.linearLayout);
dialog=new Dialog(this);
String[] cities=getResources().getStringArray(R.array.cities);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(PrayerActivity.this,android.R.layout.simple_list_item_1,cities);
search.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
btnMethod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMethod();
}
});
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SearchAPI();
}
});
search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
keyboard();
search.setCursorVisible(true);
String selectedData=parent.getItemAtPosition(position).toString();
ArrayAPI(selectedData);
}
});
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideSoftKeyboard(PrayerActivity.this);
}
});
}
private void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
@Override
public void onBackPressed() {
finish();
startActivity(new Intent(PrayerActivity.this, MainActivity.class));
Animatoo.animateSlideRight(PrayerActivity.this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
startActivity(new Intent(PrayerActivity.this, MainActivity.class));
Animatoo.animateSlideRight(PrayerActivity.this);
}
return super.onOptionsItemSelected(item);
}
private void ArrayAPI(String passData) {
SharedPreferences sharedPreferences=getSharedPreferences("METHOD", Context.MODE_PRIVATE);
SharedPreferences sharedPreferences1=getSharedPreferences("SCHOOL", Context.MODE_PRIVATE);
int method = sharedPreferences.getInt("key",4);
int school = sharedPreferences1.getInt("keyS",0);
keyboard();
if (passData.isEmpty()){
Toast.makeText(PrayerActivity.this,"Please enter city name",Toast.LENGTH_SHORT).show();
}else {
final ProgressDialog pDialog = new ProgressDialog(PrayerActivity.this);
pDialog.setMessage("Loading...");
pDialog.show();
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(APIInterface.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIInterface apiInterface=retrofit.create(APIInterface.class);
Call<Prayer> call=apiInterface.getPrayer(passData,method,school);
call.enqueue(new Callback<Prayer>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(Call<Prayer> call, Response<Prayer> response) {
Prayer prayer=response.body();
String fajrMain=prayer.getData().getTimings().getFajr();
String dhuhrMain=prayer.getData().getTimings().getDhuhr();
String asrMain=prayer.getData().getTimings().getAsr();
String maghribMain=prayer.getData().getTimings().getMaghrib();
String ishaMain=prayer.getData().getTimings().getIsha();
String dateMain=prayer.getData().getDate().getReadable();
double locationMainLat=prayer.getData().getMeta().getLatitude();
double locationMainLong=prayer.getData().getMeta().getLongitude();
LocalTime localTimeFajr = LocalTime.parse(fajrMain, DateTimeFormatter.ofPattern("HH:mm")); //In this line it is getting crashed
String fajrResult=localTimeFajr.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeDhuhr = LocalTime.parse(dhuhrMain, DateTimeFormatter.ofPattern("HH:mm"));
String dhuhrResult=localTimeDhuhr.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeAsr = LocalTime.parse(asrMain, DateTimeFormatter.ofPattern("HH:mm"));
String asrResult=localTimeAsr.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeMaghrib = LocalTime.parse(maghribMain, DateTimeFormatter.ofPattern("HH:mm"));
String maghribResult=localTimeMaghrib.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeIsha = LocalTime.parse(ishaMain, DateTimeFormatter.ofPattern("HH:mm"));
String ishaResult=localTimeIsha.format(DateTimeFormatter.ofPattern("hh:mm a"));
fajrTv.setText(fajrResult);
dhuhrTv.setText(dhuhrResult);
asrTv.setText(asrResult);
maghribTv.setText(maghribResult);
ishaTv.setText(ishaResult);
String city = "";
String country="";
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(PrayerActivity.this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(locationMainLat, locationMainLong, 1);
city=addresses.get(0).getLocality();
country=addresses.get(0).getCountryName();
} catch (IOException e) {
e.printStackTrace();
}
dateTv.setText(dateMain);
locationTv.setText(city+", "+country);
pDialog.hide();
}
@Override
public void onFailure(Call<Prayer> call, Throwable t) {
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
private void SearchAPI() {
SharedPreferences sharedPreferences=getSharedPreferences("METHOD", Context.MODE_PRIVATE);
SharedPreferences sharedPreferences1=getSharedPreferences("SCHOOL", Context.MODE_PRIVATE);
String mLocation=search.getText().toString().trim();
int method = sharedPreferences.getInt("key",4);
int school = sharedPreferences1.getInt("keyS",0);
keyboard();
if (mLocation.isEmpty()){
Toast.makeText(PrayerActivity.this,"Please enter city name",Toast.LENGTH_SHORT).show();
}else {
final ProgressDialog pDialog = new ProgressDialog(PrayerActivity.this);
pDialog.setMessage("Loading...");
pDialog.show();
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(APIInterface.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIInterface apiInterface=retrofit.create(APIInterface.class);
Call<Prayer> call=apiInterface.getPrayer(mLocation,method,school);
call.enqueue(new Callback<Prayer>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(Call<Prayer> call, Response<Prayer> response) {
Prayer prayer=response.body();
String fajrMain=prayer.getData().getTimings().getFajr();
String dhuhrMain=prayer.getData().getTimings().getDhuhr();
String asrMain=prayer.getData().getTimings().getAsr();
String maghribMain=prayer.getData().getTimings().getMaghrib();
String ishaMain=prayer.getData().getTimings().getIsha();
String dateMain=prayer.getData().getDate().getReadable();
double locationMainLat=prayer.getData().getMeta().getLatitude();
double locationMainLong=prayer.getData().getMeta().getLongitude();
LocalTime localTimeFajr = LocalTime.parse(fajrMain, DateTimeFormatter.ofPattern("HH:mm"));
String fajrResult=localTimeFajr.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeDhuhr = LocalTime.parse(dhuhrMain, DateTimeFormatter.ofPattern("HH:mm"));
String dhuhrResult=localTimeDhuhr.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeAsr = LocalTime.parse(asrMain, DateTimeFormatter.ofPattern("HH:mm"));
String asrResult=localTimeAsr.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeMaghrib = LocalTime.parse(maghribMain, DateTimeFormatter.ofPattern("HH:mm"));
String maghribResult=localTimeMaghrib.format(DateTimeFormatter.ofPattern("hh:mm a"));
LocalTime localTimeIsha = LocalTime.parse(ishaMain, DateTimeFormatter.ofPattern("HH:mm"));
String ishaResult=localTimeIsha.format(DateTimeFormatter.ofPattern("hh:mm a"));
fajrTv.setText(fajrResult);
dhuhrTv.setText(dhuhrResult);
asrTv.setText(asrResult);
maghribTv.setText(maghribResult);
ishaTv.setText(ishaResult);
String city = "";
String country="";
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(PrayerActivity.this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(locationMainLat, locationMainLong, 1);
city=addresses.get(0).getLocality();
country=addresses.get(0).getCountryName();
} catch (IOException e) {
e.printStackTrace();
}
dateTv.setText(dateMain);
locationTv.setText(city+", "+country);
pDialog.hide();
}
@Override
public void onFailure(Call<Prayer> call, Throwable t) {
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
}
LOGCAT
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nabil.myapplication, PID: 1675
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/format/DateTimeFormatter;
at com.nabil.myapplication.PrayerActivity$5.onResponse(PrayerActivity.java:164)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)