在 Android Studio 中,我的 Toast 消息有问题
In Android Studio there is a problem in my Toast message
我在 Android Studio 中有项目 有 RecyclerView ,模型 class 是: item (String name , int image) ,我想显示 Toast 消息包括 item_name 当我点击该项目时。
但那是 运行 它停止的应用程序。我不知道我的代码有什么错误
你能帮帮我吗?
模型class
public class item {
String name ;
int image ;
public item(String name, int image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
一个接口
public interface item_OnClick {
void OnClick(item i);
}
item_adapter
public class item_adapter extends RecyclerView.Adapter<item_adapter.item_view_holder> {
ArrayList<item> places ;
item_OnClick listener;
public item_adapter(ArrayList<item> places, item_OnClick listener) {
this.places = places;
this.listener = listener;
}
@NonNull
@Override
public item_view_holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_design,parent,false);
item_view_holder holder = new item_view_holder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull item_view_holder holder, int position) {
item i = places.get(position);
holder.name.setText(i.getName());
holder.img.setImageResource(i.getImage());
}
@Override
public int getItemCount() {
return places.size();
}
class item_view_holder extends RecyclerView.ViewHolder {
TextView name ;
ImageView img ;
item i ;
public item_view_holder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.item_name);
img=itemView.findViewById(R.id.item_img1);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.OnClick(i);
}
});
}
}
}
主要Activity
public class MainActivity extends AppCompatActivity implements item_OnClick{
RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
ArrayList<item> places = new ArrayList<>();
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف الاقصر" ,R.drawable.aqsar));
places.add(new item("متحف الاقصر" ,R.drawable.aqsar));
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف قصر هشام" ,R.drawable.hisham_museum));
places.add(new item("متحف قصر هشام" ,R.drawable.hisham_museum));
item_adapter adapter = new item_adapter(places,this);
rv.setAdapter(adapter);
GridLayoutManager lm=new GridLayoutManager(this,2);
rv.setLayoutManager(lm);
rv.setHasFixedSize(true);
}
@Override
public void OnClick(item i) {
Toast.makeText(this, i.getName(), Toast.LENGTH_SHORT).show();
}
}
错误
09-25 18:55:31.490 10410-10410/? E/libprocessgroup: failed to make and chown /acct/uid_10064: Read-only file system
09-25 18:55:42.377 10410-10410/com.zahraa.j_hw1_recyclerview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zahraa.j_hw1_recyclerview, PID: 10410
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.zahraa.j_hw1_recyclerview.item.getName()' on a null object reference
at com.zahraa.j_hw1_recyclerview.MainActivity.OnClick(MainActivity.java:48)
at com.zahraa.j_hw1_recyclerview.item_adapter$item_view_holder.onClick(item_adapter.java:55)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
有什么问题吗?
你发送listener.OnClick(i);
,但是没有你分配的地方i
。 (onBindViewHolder
里面的item i
是局部变量,不是ViewHolder
的字段
改用 listener.OnClick(places.get(getAdapterPosition()));
或在 onBindViewHolder
:
中分配 i
@Override
public void onBindViewHolder(@NonNull item_view_holder holder, int position) {
hodler.i = places.get(position);
holder.name.setText(i.getName());
holder.img.setImageResource(i.getImage());
}
我在 Android Studio 中有项目 有 RecyclerView ,模型 class 是: item (String name , int image) ,我想显示 Toast 消息包括 item_name 当我点击该项目时。 但那是 运行 它停止的应用程序。我不知道我的代码有什么错误 你能帮帮我吗?
模型class
public class item {
String name ;
int image ;
public item(String name, int image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
一个接口
public interface item_OnClick {
void OnClick(item i);
}
item_adapter
public class item_adapter extends RecyclerView.Adapter<item_adapter.item_view_holder> {
ArrayList<item> places ;
item_OnClick listener;
public item_adapter(ArrayList<item> places, item_OnClick listener) {
this.places = places;
this.listener = listener;
}
@NonNull
@Override
public item_view_holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_design,parent,false);
item_view_holder holder = new item_view_holder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull item_view_holder holder, int position) {
item i = places.get(position);
holder.name.setText(i.getName());
holder.img.setImageResource(i.getImage());
}
@Override
public int getItemCount() {
return places.size();
}
class item_view_holder extends RecyclerView.ViewHolder {
TextView name ;
ImageView img ;
item i ;
public item_view_holder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.item_name);
img=itemView.findViewById(R.id.item_img1);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.OnClick(i);
}
});
}
}
}
主要Activity
public class MainActivity extends AppCompatActivity implements item_OnClick{
RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
ArrayList<item> places = new ArrayList<>();
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف الاقصر" ,R.drawable.aqsar));
places.add(new item("متحف الاقصر" ,R.drawable.aqsar));
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف اللوفر" ,R.drawable.lofar));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف البتراء" ,R.drawable.batra));
places.add(new item("متحف قصر هشام" ,R.drawable.hisham_museum));
places.add(new item("متحف قصر هشام" ,R.drawable.hisham_museum));
item_adapter adapter = new item_adapter(places,this);
rv.setAdapter(adapter);
GridLayoutManager lm=new GridLayoutManager(this,2);
rv.setLayoutManager(lm);
rv.setHasFixedSize(true);
}
@Override
public void OnClick(item i) {
Toast.makeText(this, i.getName(), Toast.LENGTH_SHORT).show();
}
}
错误
09-25 18:55:31.490 10410-10410/? E/libprocessgroup: failed to make and chown /acct/uid_10064: Read-only file system
09-25 18:55:42.377 10410-10410/com.zahraa.j_hw1_recyclerview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zahraa.j_hw1_recyclerview, PID: 10410
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.zahraa.j_hw1_recyclerview.item.getName()' on a null object reference
at com.zahraa.j_hw1_recyclerview.MainActivity.OnClick(MainActivity.java:48)
at com.zahraa.j_hw1_recyclerview.item_adapter$item_view_holder.onClick(item_adapter.java:55)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
有什么问题吗?
你发送listener.OnClick(i);
,但是没有你分配的地方i
。 (onBindViewHolder
里面的item i
是局部变量,不是ViewHolder
改用 listener.OnClick(places.get(getAdapterPosition()));
或在 onBindViewHolder
:
@Override
public void onBindViewHolder(@NonNull item_view_holder holder, int position) {
hodler.i = places.get(position);
holder.name.setText(i.getName());
holder.img.setImageResource(i.getImage());
}