我试图从我的RecyclerView.ViewHolder上点击方法的其他片段中打开一个新片段,我从firebase中检索我的数据。
但它抛出了下一个错误:
java.lang.NullPointerException:尝试调用接口方法' void es.laramira.atellez.moroninfo.NewsClickedListener.onClick(android.view.View)'在null对象引用上 在es.laramira.atellez.moroninfo.Models.NewsViewHolder $ 1.onClick(NewsViewHolder.java:27) 在android.view.View.performClick(View.java:5610) 在android.view.View $ PerformClick.run(View.java:22265) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我创建了一个界面:
package es.laramira.atellez.moroninfo;
import android.view.View;
public interface NewsClickedListener {
void onClick(View v);
}
此接口在新的AppCompatActivity中实现:
package es.laramira.atellez.moroninfo;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;
import android.view.View;
import es.laramira.atellez.moroninfo.Fragments.DetalleFragment;
public class NewsActivity extends AppCompatActivity implements NewsClickedListener {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
DetalleFragment df = new DetalleFragment();
df.setArguments(args);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_detalle,df);
ft.setTransition(ft.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
我在这个类中实例化它,实现它的方法:
package es.laramira.atellez.moroninfo.Models;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import es.laramira.atellez.moroninfo.NewsClickedListener;
import es.laramira.atellez.moroninfo.R;
public class NewsViewHolder extends RecyclerView.ViewHolder{
View mView;
NewsClickedListener newsClickedListener;
public NewsViewHolder(final View itemView) {
super(itemView);
mView = itemView;
itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
newsClickedListener.onClick(view);
}
});
}
//Datos que obtenemos de firebase y los usamos en la cardview, se llaman desde cada categoria
public void setTitular(String titular){
TextView post_titular = (TextView) mView.findViewById(R.id.titleText);
post_titular.setText(titular);
}
public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.imageView);
Picasso.with(ctx).load(image).into(post_image);
}
//Datos para el detalle, lo llama el fragmento de detalle y dependera de cada opcion elegida
public void setTitularDetalle(String titular){
TextView post_titular_detalle = (TextView) mView.findViewById(R.id.txtCabeceraDetalle);
post_titular_detalle.setText(titular);
}
}
答案 0 :(得分:0)
您必须使用回调方法告知Activity
主机Fragment1
更改片段。
创建一个侦听器,例如:
public interface NewsClickedListener
{
void onClick(View v);
}
将由承载两个片段的Activity
实现:
public class NewsActivity extends AppCompatActivity implements NewsClickedListener
{
...
@Override
public void onClick(View v)
{
Bundle args = new Bundle();
DetalleFragment df = new DetalleFragment();
df.setArguments(args);
FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
fm.replace(R.id.fragment_detalle,df);
fm.setTransition(fm.TRANSIT_FRAGMENT_OPEN);
fm.addToBackStack(null);
fm.commit();
}
}
然后您必须在NewsClickedListener
中添加Fragment1
个实例,然后像这样更改itemView.setOnClickListener
:
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
newsClickedListenerInstance.onClick(v);
}
});