以下是我的问题,我尝试制作新闻App,并尝试制作显示时间格式的时间格式,例如twitter。我使用了Prettytime库并设置了SimpleDataFormat实例甚至使其具有可读性,但是当我设置日期实例来解析simpleDataFormat实例时,我遇到了这个错误
我的错误代码:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 3263
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.icu.text.SimpleDateFormat.subParse(SimpleDateFormat.java:3131)
at android.icu.text.SimpleDateFormat.parse(SimpleDateFormat.java:2443)
at android.icu.text.DateFormat.parse(DateFormat.java:752)
at android.icu.text.DateFormat.parse(DateFormat.java:667)
at com.example.myapplication.AdapterR.getExatTime(AdapterR.java:63)
at com.example.myapplication.AdapterR.onBindViewHolder(AdapterR.java:111)
at com.example.myapplication.AdapterR.onBindViewHolder(AdapterR.java:40)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1775)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:754)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2991)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2504)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1635)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7795)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1172)
at android.view.Choreographer.doCallbacks(Choreographer.java:984)
at android.view.Choreographer.doFrame(Choreographer.java:809)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1158)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 3263
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.icu.text.SimpleDateFormat.subParse(SimpleDateFormat.java:3131)
at android.icu.text.SimpleDateFormat.parse(SimpleDateFormat.java:2443)
at android.icu.text.DateFormat.parse(DateFormat.java:752)
at android.icu.text.DateFormat.parse(DateFormat.java:667)
at com.example.myapplication.AdapterR.getExatTime(AdapterR.java:63)
at com.example.myapplication.AdapterR.onBindViewHolder(AdapterR.java:111)
at com.example.myapplication.AdapterR.onBindViewHolder(AdapterR.java:40)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1775)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:754)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2991)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2504)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1635)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7795)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1172)
at android.view.Choreographer.doCallbacks(Choreographer.java:984)
at android.view.Choreographer.doFrame(Choreographer.java:809)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1158)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W/OPDiagnose: getService:OPDiagnoseService NULL
D/OSTracker: OS Event: crash
I/Process: Sending signal. PID: 3263 SIG: 9
Disconnected from the target VM, address: 'localhost:8603', transport: 'socket'
这是我的适配器代码:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.icu.text.SimpleDateFormat;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import org.ocpsoft.prettytime.PrettyTime;
import java.net.URL;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
public class AdapterR extends RecyclerView.Adapter<AdapterR.ViewHolderClass> {
private List<Articles> mylistAdapter;
private List<Articles> mSearchItems;
private Context mcontext;
private CardView myCard;
String originalDateTimeFormat;
private SharedPreferences sharedPreferences;
// public OnItemClickListener OnItemClickListener;
// public void setOnItemClickListener(FragmentActivity onItemClickListener){
// this.OnItemClickListener=onItemClickListener;
// }
public AdapterR(Context context, List<Articles> myList) {
this.mylistAdapter = myList;
this.mcontext = context;
this.mSearchItems = new ArrayList<>(mylistAdapter);
}
private String getExatTime(String timeAgo) {
String timeIn=null;
PrettyTime prettyTime=new PrettyTime(Locale.getDefault());
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd:'T' HH:mm:ss'Z'",Locale.ENGLISH);
try {
Date date=simpleDateFormat.parse(timeAgo);
timeIn=prettyTime.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return timeIn;
}
public void clear() {
mylistAdapter.clear();
notifyDataSetChanged();
}
public void addAll(List<Articles> list) {
mylistAdapter.addAll(list);
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolderClass onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(mcontext).inflate(R.layout.items_style, viewGroup, false);
return new ViewHolderClass(v);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolderClass viewHolderClass, final int i) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mcontext);
viewHolderClass.title.setText(mylistAdapter.get(i).getTitle());
// viewHolderClass.des.setText(mylistAdapter.get(i).getDescription());
// viewHolderClass.sourcetxt.setText(mylistAdapter.get(i).getMsource());
// long timeFormated = FormatTime();
// CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(timeFormated);
// String[] parts=originalDateTimeFormat.split("T");
// String date=parts[0];
// String unformattedTime=parts[1];
// String time=unformattedTime.substring(0,unformattedTime.length()-1);
// viewHolderClass.publichdata.setText(date);
viewHolderClass.publichtime.setText(" \u2022 " + getExatTime(originalDateTimeFormat));
if (!mylistAdapter.get(i).getUrlToImage().isEmpty() || mylistAdapter.get(i).getUrlToImage() != null) {
// Glide.with(mcontext).load(mylistAdapter.get(i).getUrlToImage()).into(viewHolderClass.imageView);
Picasso.get().load(mylistAdapter.get(i).getUrlToImage()).into(viewHolderClass.imageView);
} else {
viewHolderClass.imageView.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return mylistAdapter.size();
}
//
// @Override
// public Filter getFilter() {
// return mSearchItemsflter;
// }
//
// private Filter mSearchItemsflter=new Filter() {
// @Override
// protected FilterResults performFiltering(CharSequence constraint) {
// List<Articles> listFilter=new ArrayList<>();
// if (constraint==null||constraint.length()==0){
// listFilter.addAll(mSearchItems);
// }else {
// String filterPattren = constraint.toString().toLowerCase().trim();
// for (Articles articles : mSearchItems){
// if (articles.getTitle().toLowerCase().startsWith(filterPattren)){
// listFilter.add(articles);
//
// }
//
// }
//
// }
//
// FilterResults filterResults=new FilterResults();
// filterResults.values= listFilter;
// return filterResults;
// }
//
//
// @Override
// protected void publishResults(CharSequence constraint, FilterResults results) {
// mylistAdapter.clear();
// mylistAdapter.addAll((List)results.values);
// notifyDataSetChanged();
//
// }
// };
//
public class ViewHolderClass extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title, des, publichtime, publichdata, sourcetxt, url, autor;
// private CardView mycard;
private ImageView imageView;
// private OnItemClickListener onItemClickListener;
public ViewHolderClass(@NonNull View itemView) {
super(itemView);
// this.onItemClickListener=onItemClickListener;
title = itemView.findViewById(R.id.titleNew);
// des=itemView.findViewById(R.id.description);
// publichdata=itemView.findViewById(R.id.date);
publichtime = itemView.findViewById(R.id.time);
imageView = itemView.findViewById(R.id.imageview);
itemView.setOnClickListener(this);
// sourcetxt=itemView.findViewById(R.id.sourceid);
// mycard=(CardView) itemView.findViewById(R.id.card);
// itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int i = getAdapterPosition();
Articles articles = mylistAdapter.get(i);
Intent intent = new Intent(mcontext, Web.class);
intent.putExtra("imageTitle", articles.getUrlToImage());
intent.putExtra("urltext", articles.getUrl());
mcontext.startActivity(intent);
}
// @Override
// public void onClick(View v) {
// onItemClickListener.OnItemClick(v,getAdapterPosition());
// }
}
// public interface OnItemClickListener{
// void OnItemClick(View v,int pos);
// }
// public long FormatTime(){
//
// DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS'Z'", Locale.ENGLISH);
// DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy", Locale.ENGLISH);
// LocalDate date = LocalDate.parse(originalDateTimeFormat, inputFormatter);
//
// return Long.parseLong(outputFormatter.format(date));
//
// }
}