Java 我需要帮助来修复这个NPE
我试图从捆绑包中适配器的intent中获取一个值,但它似乎给了我一个null指针异常 我不确定,据我所知,“获取额外捆绑包”给了我一个NPE,但不确定如何修复它 //首先是适配器 公共类ArticleAdapter扩展了RecyclerView.Adapter{Java 我需要帮助来修复这个NPE,java,nullpointerexception,Java,Nullpointerexception,我试图从捆绑包中适配器的intent中获取一个值,但它似乎给了我一个null指针异常 我不确定,据我所知,“获取额外捆绑包”给了我一个NPE,但不确定如何修复它 //首先是适配器 公共类ArticleAdapter扩展了RecyclerView.Adapter{ class MyViewHolder extends RecyclerView.ViewHolder { TextView category; TextView title;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView category;
TextView title;
ImageView image;
TextView pubDate;
MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.row_title);
image = (ImageView) view.findViewById(R.id.row_image);
pubDate = (TextView) view.findViewById(R.id.row_pubDate);
category = (TextView) view.findViewById(R.id.row_categories);
}
}
@NotNull
@Override
public MyViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row,parent, false);
return new MyViewHolder(v);
}
private List<Article> articles;
private Context mContext;
private WebView articleView;
public ArticleAdapter(List<Article> list, Context context) {
this.articles = list;
this.mContext = context;
}
public List<Article> getArticleList() {
return articles;
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder viewHolder, int position) {
Article currentArticle = articles.get(position);
Log.e("article", currentArticle.getTitle());
String pubDateString;
try {
String sourceDateString = currentArticle.getPubDate();
SimpleDateFormat sourceSdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Date date = sourceSdf.parse(sourceDateString);
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
pubDateString = sdf.format(date);
} catch (ParseException e) {
e.printStackTrace();
pubDateString = currentArticle.getPubDate();
}
viewHolder.title.setText(currentArticle.getTitle());
Picasso.get()
.load(currentArticle.getImage())
.placeholder(R.drawable.placeholder)
.into(viewHolder.image);
viewHolder.pubDate.setText(pubDateString);
StringBuilder categories = new StringBuilder();
for (int i = 0; i < currentArticle.getCategories().size(); i++) {
if (i == currentArticle.getCategories().size() - 1) {
categories.append(currentArticle.getCategories().get(i));
} else {
categories.append(currentArticle.getCategories().get(i)).append(", ");
}
}
viewHolder.category.setText(categories.toString());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onClick(View view) {
articleView = new WebView(mContext);
articleView.getSettings().setLoadWithOverviewMode(true);
String title = articles.get(viewHolder.getAdapterPosition()).getTitle();
String content = articles.get(viewHolder.getAdapterPosition()).getContent();
articleView.getSettings().setJavaScriptEnabled(true);
articleView.setHorizontalScrollBarEnabled(false);
articleView.setWebChromeClient(new WebChromeClient());
articleView.loadDataWithBaseURL(null, "<style>img{display: inline; height: auto; max-width: 100%;} " +
"</style>\n" + "<style>iframe{ height: auto; width: auto;}" + "</style>\n" + content, null, "utf-8", null);
Intent intent = new Intent(mContext,DetailActivity.class);
intent.putExtra("setTitle",title);
intent.putExtra("setContent",content);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return articles == null ? 0 : articles.size();
}
}
//the Activity code
public class DetailActivity extends AppCompatActivity {
DetailFragment mDetailFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
DetailFragment mDetailFragment = (DetailFragment)getSupportFragmentManager().findFragmentByTag("TAG");
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
mDetailFragment.setTitle((String) bundle.get("setTitle"));
showDetailFragment();
}
private void startTransactionFragment(Fragment fragment) {
if (!fragment.isVisible()) {
getSupportFragmentManager().beginTransaction().add(R.id.detail_activity_frame_layout, fragment).commit();
}
}
private void showDetailFragment() {
if (this.mDetailFragment == null) this.mDetailFragment = DetailFragment.newInstance();
this.startTransactionFragment(this.mDetailFragment);
}
}
//the fragment code
public class DetailFragment extends Fragment {
@BindView(R.id.detail_title) TextView title;
@BindView(R.id.detail_content) WebView content;
private WebView articleView;
public DetailFragment() { }
public static DetailFragment newInstance () {
return (new DetailFragment());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_detail, container, false);
ButterKnife.bind(this, view);
return view;
}
public void setTitle(String s){ title.setText(s); }
public void setContent(String s){
articleView = new WebView(getContext());
articleView.getSettings().setLoadWithOverviewMode(true);
articleView.getSettings().setJavaScriptEnabled(true);
articleView.setHorizontalScrollBarEnabled(false);
articleView.setWebChromeClient(new WebChromeClient());
articleView.loadDataWithBaseURL(null, "<style>img{display: inline; height: auto; max-width: 100%;} " +
"</style>\n" + "<style>iframe{ height: auto; width: auto;}" + "</style>\n" + s, null, "utf-8", null);
}
}
//and finally the xml code for both
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".Controllers.Fragments.DetailFragment"
android:tag="TAG">
<TextView
android:id="@+id/detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TITLE"
android:textSize="50dp"/>
<WebView
android:id="@+id/detail_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ARTICLE"
android:textSize="20dp"
android:layout_gravity="center"/>
</LinearLayout>
//activity xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/detail_activity_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Controllers.Activities.DetailActivity">
</FrameLayout>
MyViewHolder类扩展了RecyclerView.ViewHolder{
文本视图类别;
文本视图标题;
图像视图图像;
文本视图发布日期;
MyViewHolder(视图){
超级(视图);
title=(TextView)view.findViewById(R.id.row\u title);
image=(ImageView)view.findviewbyd(R.id.row\u image);
pubDate=(TextView)view.findViewById(R.id.row\u pubDate);
category=(TextView)view.findViewById(R.id.row\u categories);
}
}
@NotNull
@凌驾
公共MyViewHolder onCreateViewHolder(@NotNull ViewGroup父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext())
.充气(右布局行,父级,假);
返回新的MyViewHolder(v);
}
私人物品清单;
私有上下文;
私有网络视图articleView;
公共ArticleAdapter(列表、上下文){
这个.文章=列表;
this.mContext=上下文;
}
公共列表getArticleList(){
归还物品;
}
@凌驾
public void onBindViewHolder(@NonNull final MyViewHolder viewHolder,int位置){
Article currentArticle=articles.get(位置);
Log.e(“article”,currentArticle.getTitle());
字符串pubDateString;
试一试{
String sourceDateString=currentArticle.getPubDate();
SimpleDataFormat sourceSdf=新的SimpleDataFormat(“EEE,d MMM yyy HH:mm:ss Z”,Locale.ENGLISH);
Date-Date=sourceSdf.parse(sourceDateString);
SimpleDataFormat sdf=新的SimpleDataFormat(“dd-MMMM-yyyy”,Locale.getDefault());
pubDateString=sdf.format(日期);
}捕获(解析异常){
e、 printStackTrace();
pubDateString=currentArticle.getPubDate();
}
viewHolder.title.setText(currentArticle.getTitle());
毕加索
.load(currentArticle.getImage())
.placeholder(R.drawable.placeholder)
.into(viewHolder.image);
viewHolder.pubDate.setText(pubDateString);
StringBuilder类别=新建StringBuilder();
对于(int i=0;i// Let's first dynamically add a fragment into a frame container, that is your xml like a LinearLayout
getSupportFragmentManager().beginTransaction().
replace(R.id.YOUR_XML, new DetailFragment(), "SOMETAG").
commit();
// Now later we can lookup the fragment by tag
DetailFragment mDetailFragment = (DetailFragment)
getSupportFragmentManager().findFragmentByTag("SOMETAG");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
DetailFragment fragment = (DetailFragment)getSupportFragmentManager().findFragmentByTag("TAG");
if(fragment == NULL){
fragment = DetailFragment.newInstance();
}
fragment.setTitle((String) bundle.get("setTitle"));
startTransactionFragment(fragment);
}