我有一个充满我的RecyclerView的适配器。 由于onClick,当我单击RecyclerView的Item时,我存储了与查询匹配的数据库数据并启动了新的Activity。例如,我想用来自“名称”列的数据填充一个Activity的TextView。但是,当我单击RecyclerView的Item时,应用程序崩溃。我怎么解决这个问题? (如果需要,我可以提供更多代码或信息。)
MyAdapter.java代码:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
private ArrayList<MyObject> mItems = new ArrayList<>();
public String nametext;
DatabaseHelper myDB;
Main3Activity my3;
int idactivity;
String nameactivity;
int yearactivity;
int monthactivity;
int dayactivity;**
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
MyViewHolder vh=new MyViewHolder(view);
view.setOnClickListener(this);
return vh;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.bindData(getItem(position));
}
@Override
public int getItemCount() {
return mItems.size();
}
public void updateData(ArrayList<MyObject> items) {
mItems = new ArrayList<>(items);
notifyDataSetChanged();
}
private MyObject getItem(int position) {
if (position >= 0 && position < mItems.size())
return mItems.get(position);
return null;
}
@Override
public void onClick(View v) {
idactivity = 0;
nameactivity = null;
yearactivity = 0;
monthactivity = 0;
dayactivity = 0;
myDB = new DatabaseHelper(v.getContext());
my3 = new Main3Activity();
TextView clickedName=(TextView)v.findViewById(R.id.title);
nametext = clickedName.getText().toString();
Toast.makeText(v.getContext(),nametext,Toast.LENGTH_LONG).show();
Cursor c = myDB.showDataonSelect(nametext);
if(c.moveToFirst()) {
idactivity = c.getInt(0) ;
nameactivity = c.getString(1) ;
yearactivity = c.getInt(2) ;
monthactivity = c.getInt(3) ;
dayactivity = c.getInt(4);
}
TextView txt1 = my3.getTxt1();
txt1.setText(nameactivity);
Intent myIntent = new Intent(v.getContext(), Main3Activity.class);
v.getContext().startActivity(myIntent);
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView subTitle;
MyViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
subTitle = itemView.findViewById(R.id.sub_title);
}
void bindData(MyObject myObject) {
if (myObject == null)
return;
title.setText(myObject.title);
subTitle.setText(myObject.subTitle);
}
} }
Main3Activity.java代码:
public class Main3Activity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
public TextView getTxt1(){
TextView txt1 = findViewById(R.id.textView3);
return txt1;
}}
logcat带有崩溃时出现的异常消息:
2019-06-06 18:12:27.721 15509-15509/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 15509
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.myapplication.Main3Activity.getTxt1(Main3Activity.java:18)
at com.example.myapplication.MyAdapter.onClick(MyAdapter.java:90)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
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:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我想从MyAdapter的onClick方法填充Activity的TextView。
答案 0 :(得分:1)
似乎您想在用户使用项目标题单击某个项目时打开Main3Activity。这样做的方法是向活动传递额外的东西(也称为片段参数)
首先,您应该在MyViewHolder中实现View.OnClickListener:
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
还将其从MyAdapter中删除:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
然后从MyViewHolder内的MyAdapter中移动onClick方法
同样在onClick方法中,不要使用Main3Activity构造函数。只需使用Intent并在标题上添加额外的内容即可。
@Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Main3Activity.class);
myIntent.putExtra("TITLE_EXTRA", title.getText().toString())
v.getContext().startActivity(myIntent);
}
onClick方法中的其他任何内容都将其移至Main3Activity的onCreate或任何其他方法中。
要在Main3Activity中获取TITLE_EXTRA,请将其添加到您的onCreate方法中:
private String title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
...
if(getIntent() != null) {
title = getIntent().getStringExtra("TITLE_EXTRA")
}
...
}
答案 1 :(得分:0)
从onClick中删除TextView TextView clickedName=(TextView)v.findViewById(R.id.title);
的初始化,并在Holder类中对其进行初始化。